简介
本文用示例介绍HashMap排序的方法。
排序已有数据
按key排序
使用stream进行排序(按key升序/降序)
package org.example.a; import java.util.*; public class Demo { public static void main(String[] args) { Mapmap = new HashMap<>(); map.put("ad", "dd"); map.put("bc", "ee"); map.put("cb", "ff"); for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); Map linkedHashMap = new LinkedHashMap<>(); // 默认按照升序排列 map.entrySet().stream().sorted(Map.Entry.comparingByKey()) .forEach(o -> linkedHashMap.put(o.getKey(), o.getValue())); for (Map.Entry entry : linkedHashMap.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); Map linkedHashMap1 = new LinkedHashMap<>(); // 自定义排序(降序) map.entrySet().stream().sorted(Map.Entry.comparingByKey(new Comparator () { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } })).forEach(o -> linkedHashMap1.put(o.getKey(), o.getValue())); for (Map.Entry 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) { Mapmap = new HashMap<>(); map.put("ad", "dd"); map.put("bc", "ee"); map.put("cb", "ff"); for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); // 默认按照升序排序 Map map1 = new TreeMap<>(); map.forEach(map1::put); for (Map.Entry entry : map1.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); // 自定义排序(降序) Map map2 = new TreeMap<>(new Comparator () { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }); map.forEach(map2::put); for (Map.Entry 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) { Mapmap = new HashMap<>(); map.put("ad", "dd"); map.put("bc", "ee"); map.put("cb", "ff"); for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); Map linkedHashMap = new LinkedHashMap<>(); // 默认按照升序排列 map.entrySet().stream().sorted(Map.Entry.comparingByValue()) .forEach(o -> linkedHashMap.put(o.getKey(), o.getValue())); for (Map.Entry entry : linkedHashMap.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); Map linkedHashMap1 = new LinkedHashMap<>(); // 自定义排序(降序) map.entrySet().stream().sorted(Map.Entry.comparingByValue(new Comparator () { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } })).forEach(o -> linkedHashMap1.put(o.getKey(), o.getValue())); for (Map.Entry 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
本处只写升序代码,降序只是调换个顺序而已。
package org.example.a; import java.util.*; public class Demo { public static void main(String[] args) { Mapmap = new HashMap<>(); map.put("ad", "dd"); map.put("bc", "ee"); map.put("cb", "ff"); for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println(); Map sortedMap = new LinkedHashMap<>(); List > entryList = new ArrayList >( map.entrySet()); Collections.sort(entryList, new Comparator >() { @Override public int compare(Map.Entry me1, Map.Entry me2) { return me1.getValue().compareTo(me2.getValue()); } }); for (Map.Entry stringStringEntry : entryList) { sortedMap.put(stringStringEntry.getKey(), stringStringEntry.getValue()); } for (Map.Entry 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) { MaphashMap = new HashMap (); hashMap.put("name1", "josan1"); hashMap.put("name2", "josan2"); hashMap.put("name3", "josan3"); Set > set = hashMap.entrySet(); Iterator > 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) { MaphashMap = new LinkedHashMap (); hashMap.put("name1", "josan1"); hashMap.put("name2", "josan2"); hashMap.put("name3", "josan3"); Set > set = hashMap.entrySet(); Iterator > 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排序的资料请关注脚本之家其它相关文章!