您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java如何實現(xiàn)HashMap排序的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Java如何實現(xiàn)HashMap排序文章都會有所收獲,下面我們一起來看看吧。
使用stream進行排序(按key升序/降序)
package org.example.a; import java.util.*; public class Demo { public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("ad", "dd"); map.put("bc", "ee"); map.put("cb", "ff"); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); Map<String, String> linkedHashMap = new LinkedHashMap<>(); // 默認按照升序排列 map.entrySet().stream().sorted(Map.Entry.comparingByKey()) .forEach(o -> linkedHashMap.put(o.getKey(), o.getValue())); for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); Map<String, String> linkedHashMap1 = new LinkedHashMap<>(); // 自定義排序(降序) map.entrySet().stream().sorted(Map.Entry.comparingByKey(new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } })).forEach(o -> linkedHashMap1.put(o.getKey(), o.getValue())); for (Map.Entry<String, String> entry : linkedHashMap1.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } }
執(zhí)行結(jié)果
bc:ee
ad:dd
cb:ff
ad:dd
bc:ee
cb:ff
cb:ff
bc:ee
ad:dd
HashMap轉(zhuǎn)TreeMap自定義排序(按key升序/降序)
package org.example.a; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class Demo { public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("ad", "dd"); map.put("bc", "ee"); map.put("cb", "ff"); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); // 默認按照升序排序 Map<String, String> map1 = new TreeMap<>(); map.forEach(map1::put); for (Map.Entry<String, String> entry : map1.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); // 自定義排序(降序) Map<String, String> map2 = new TreeMap<>(new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }); map.forEach(map2::put); for (Map.Entry<String, String> entry : map2.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } }
執(zhí)行結(jié)果
bc:ee
ad:dd
cb:ff
ad:dd
bc:ee
cb:ff
cb:ff
bc:ee
ad:dd
使用stream進行排序(按value升序/降序)
package org.example.a; import java.util.*; public class Demo { public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("ad", "dd"); map.put("bc", "ee"); map.put("cb", "ff"); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); Map<String, String> linkedHashMap = new LinkedHashMap<>(); // 默認按照升序排列 map.entrySet().stream().sorted(Map.Entry.comparingByValue()) .forEach(o -> linkedHashMap.put(o.getKey(), o.getValue())); for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); Map<String, String> linkedHashMap1 = new LinkedHashMap<>(); // 自定義排序(降序) map.entrySet().stream().sorted(Map.Entry.comparingByValue(new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } })).forEach(o -> linkedHashMap1.put(o.getKey(), o.getValue())); for (Map.Entry<String, String> entry : linkedHashMap1.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } }
執(zhí)行結(jié)果
bc:ee
ad:dd
cb:ff
ad:dd
bc:ee
cb:ff
cb:ff
bc:ee
ad:dd
借助List進行排序(按value升序/降序)
原理:將待排序Map中的所有元素置于一個列表中,接著使用Collections的一個靜態(tài)方法 sort(List<T> list, Comparator<? super T> c) 來排序列表,同樣是用比較器定義比較規(guī)則。排序后的列表中的元素再依次裝入Map,為了肯定的保證Map中元素與排序后的List中的元素的順序一致,使用了LinkedHashMap數(shù)據(jù)類型。
本處只寫升序代碼,降序只是調(diào)換個順序而已。
package org.example.a; import java.util.*; public class Demo { public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("ad", "dd"); map.put("bc", "ee"); map.put("cb", "ff"); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); Map<String, String> sortedMap = new LinkedHashMap<>(); List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>( map.entrySet()); Collections.sort(entryList, new Comparator<Map.Entry<String, String>>() { @Override public int compare(Map.Entry<String, String> me1, Map.Entry<String, String> me2) { return me1.getValue().compareTo(me2.getValue()); } }); for (Map.Entry<String, String> stringStringEntry : entryList) { sortedMap.put(stringStringEntry.getKey(), stringStringEntry.getValue()); } for (Map.Entry<String, String> entry : sortedMap.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } }
運行結(jié)果
bc:ee
ad:dd
cb:ff
ad:dd
bc:ee
cb:ff
package org.example.a; import java.util.*; public class Demo{ public static List arrayList = new ArrayList(); public static void main(String[] args) { Map<String, String> hashMap = new HashMap<String, String>(); hashMap.put("name1", "josan1"); hashMap.put("name2", "josan2"); hashMap.put("name3", "josan3"); Set<Map.Entry<String, String>> set = hashMap.entrySet(); Iterator<Map.Entry<String, String>> iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry entry = iterator.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); System.out.println("key:" + key + ", value:" + value); } } }
執(zhí)行結(jié)果(未按照插入順序輸出)
key:name3, value:josan3
key:name2, value:josan2
key:name1, value:josan1
package org.example.a; import java.util.*; public class Demo{ public static List arrayList = new ArrayList(); public static void main(String[] args) { Map<String, String> hashMap = new LinkedHashMap<String, String>(); hashMap.put("name1", "josan1"); hashMap.put("name2", "josan2"); hashMap.put("name3", "josan3"); Set<Map.Entry<String, String>> set = hashMap.entrySet(); Iterator<Map.Entry<String, String>> iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry entry = iterator.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); System.out.println("key:" + key + ", value:" + value); } } }
執(zhí)行結(jié)果(按照插入順序輸出)
key:name1, value:josan1
key:name2, value:josan2
key:name3, value:josan3
關(guān)于“Java如何實現(xiàn)HashMap排序”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Java如何實現(xiàn)HashMap排序”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。