Java實現HashMap排序方法的示例詳解
簡介
本文用示例介紹HashMap排序的方法。
排序已有數據
按key排序
使用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()); } } }
執行結果
bc:ee
ad:dd
cb:ff
ad:dd
bc:ee
cb:ff
cb:ff
bc:ee
ad:dd
HashMap轉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()); } } }
執行結果
bc:ee
ad:dd
cb:ff
ad:dd
bc:ee
cb:ff
cb:ff
bc:ee
ad:dd
按value排序
使用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()); } } }
執行結果
bc:ee
ad:dd
cb:ff
ad:dd
bc:ee
cb:ff
cb:ff
bc:ee
ad:dd
借助List進行排序(按value升序/降序)
原理:將待排序Map中的所有元素置於一個列表中,接著使用Collections的一個靜態方法 sort(List<T> list, Comparator<? super T> c) 來排序列表,同樣是用比較器定義比較規則。排序後的列表中的元素再依次裝入Map,為瞭肯定的保證Map中元素與排序後的List中的元素的順序一致,使用瞭LinkedHashMap數據類型。
本處隻寫升序代碼,降序隻是調換個順序而已。
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()); } } }
運行結果
bc:ee
ad:dd
cb:ff
ad:dd
bc:ee
cb:ff
按插入順序存放
HashMap不按插入順序存放
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); } } }
執行結果(未按照插入順序輸出)
key:name3, value:josan3
key:name2, value:josan2
key:name1, value:josan1
LinkedHashMap會按照插入順序存放
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); } } }
執行結果(按照插入順序輸出)
key:name1, value:josan1
key:name2, value:josan2
key:name3, value:josan3
以上就是Java實現HashMap排序方法的示例詳解的詳細內容,更多關於Java HashMap排序的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- 淺談HashMap中7種遍歷方式的性能分析
- java中HashMap的7種遍歷方式與性能分析
- Java中遍歷Map集合的5種方式總結
- Java Map接口概述和常用方法詳解
- Java中Map接口使用以及有關集合的面試知識點匯總