Java TreeMap升序|降序排列和按照value進(jìn)行排序的案例
TreeMap 升序|降序排列
import java.util.Comparator; import java.util.TreeMap; public class Main { public static void main(String[] args) { TreeMap<Integer,Integer> map1 = new TreeMap<Integer,Integer>(); //默認(rèn)的TreeMap升序排列 TreeMap<Integer,Integer> map2= new TreeMap<Integer,Integer>(new Comparator<Integer>(){ /* * int compare(Object o1, Object o2) 返回一個(gè)基本類型的整型, * 返回負(fù)數(shù)表示:o1 小于o2, * 返回0 表示:o1和o2相等, * 返回正數(shù)表示:o1大于o2。 */ public int compare(Integer a,Integer b){ return b-a; } }); map2.put(1,2); map2.put(2,4); map2.put(7, 1); map2.put(5,2); System.out.println("Map2="+map2); map1.put(1,2); map1.put(2,4); map1.put(7, 1); map1.put(5,2); System.out.println("map1="+map1); } }
TreeMap按照value進(jìn)行排序
TreeMap底層是根據(jù)紅黑樹的數(shù)據(jù)結(jié)構(gòu)構(gòu)建的,默認(rèn)是根據(jù)key的自然排序來組織(比如integer的大小,String的字典排序)。所以,TreeMap只能根據(jù)key來排序,是不能根據(jù)value來排序的(否則key來排序根本就不能形成TreeMap)。
今天有個(gè)需求,就是要根據(jù)treeMap中的value排序。所以網(wǎng)上看了一下,大致的思路是把TreeMap的EntrySet轉(zhuǎn)換成list,然后使用Collections.sor排序。
代碼:
public static void sortByValue() { Map<String,String> map = new TreeMap<String,String>(); map.put("a", "dddd"); map.put("d", "aaaa"); map.put("b", "cccc"); map.put("c", "bbbb"); List<Entry<String, String>> list = new ArrayList<Entry<String, String>>(map.entrySet()); Collections.sort(list,new Comparator<Map.Entry<String,String>>() { //升序排序 public int compare(Entry<String, String> o1, Entry<String, String> o2) { return o1.getValue().compareTo(o2.getValue()); } }); for (Entry<String, String> e: list) { System.out.println(e.getKey()+":"+e.getValue()); } }
補(bǔ)充知識:使用比較器對Treemap按照value進(jìn)行排序
使用比較器對Treemap按照value進(jìn)行排序(value值只有是string類型時(shí)才適用)
有時(shí)我們需要根據(jù)TreeMap的value來進(jìn)行排序。對value排序我們就需要借助于Collections的sort(List list, Comparator
public class MapSortDemo { public static void main(String[] args) { Map<String, String> map = new TreeMap<String, String>(); map.put("KFC", "kfc"); map.put("WNBA", "wnba"); map.put("NBA", "nba"); map.put("CBA", "cba"); Map<String, String> resultMap = sortMapByKey(map); //按Key進(jìn)行排序 // Map<String, String> resultMap = sortMapByValue(map); //按Value進(jìn)行排序 for (Map.Entry<String, String> entry : resultMap.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } } /** * 使用 Map按value進(jìn)行排序 * @param map * @return */ public static Map<String, String> sortMapByValue(Map<String, String> oriMap) { if (oriMap == null || oriMap.isEmpty()) { return null; } Map<String, String> sortedMap = new LinkedHashMap<String, String>(); List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>( oriMap.entrySet()); Collections.sort(entryList, new MapValueComparator()); Iterator<Map.Entry<String, String>> iter = entryList.iterator(); Map.Entry<String, String> tmpEntry = null; while (iter.hasNext()) { tmpEntry = iter.next(); sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue()); } return sortedMap; } }
//比較器 class MapValueComparator implements Comparator<Map.Entry<String, String>> { @Override public int compare(Entry<String, String> me1, Entry<String, String> me2) { return me1.getValue().compareTo(me2.getValue()); } }
方式二
public class TreeMapTest { public static void main(String[] args) { Map<String, String> map = new TreeMap<String, String>(); map.put("a", "ddddd"); map.put("c", "bbbbb"); map.put("d", "aaaaa"); map.put("b", "ccccc"); //這里將map.entrySet()轉(zhuǎn)換成list List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(map.entrySet()); //然后通過比較器來實(shí)現(xiàn)排序 Collections.sort(list,new Comparator<Map.Entry<String,String>>() { //升序排序 public int compare(Entry<String, String> o1, Entry<String, String> o2) { return o1.getValue().compareTo(o2.getValue()); } }); for(Map.Entry<String,String> mapping:list){ System.out.println(mapping.getKey()+":"+mapping.getValue()); } } }
運(yùn)行結(jié)果如下:
d:aaaaa
c:bbbbb
b:ccccc
a:ddddd
以上這篇Java TreeMap升序|降序排列和按照value進(jìn)行排序的案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot之security?FilterSecurityInterceptor的使用要點(diǎn)記錄
這篇文章主要介紹了springboot之security?FilterSecurityInterceptor的使用要點(diǎn)記錄,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25開發(fā)環(huán)境搭
這篇文章主要介紹了spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11+mysql5.5.25開發(fā)環(huán)境搭建圖文教程,需要的朋友可以參考下2016-06-06MyBatis的CRUD中的不同參數(shù)綁定查詢實(shí)現(xiàn)
本文主要介紹了MyBatis的CRUD中的不同參數(shù)綁定查詢實(shí)現(xiàn),主要包括單個(gè)參數(shù)傳遞綁定,序號參數(shù)傳遞綁定,注解參數(shù)傳遞綁定,pojo(對象)參數(shù)傳遞綁定,map參數(shù)傳遞綁定這幾種類型,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12Go?Java算法之為運(yùn)算表達(dá)式設(shè)計(jì)優(yōu)先級實(shí)例
這篇文章主要為大家介紹了Go?Java算法之為運(yùn)算表達(dá)式設(shè)計(jì)優(yōu)先級實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08