我就废话不多说了,大家还是直接看代码吧~
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.linkedHashMap;
public class MapSorted{
public static void main(String[] args) {
Map map = new HashMap<>();
map.put("A", 3);
map.put("B", 5);
map.put("C", 1);
map.put("D", 1);
map.put("E", 9);
System.out.println(map);
//如果value为java对象,则需要实现Comparable接口,重写compareTo方法
Map sortedMap = new linkedHashMap<>();
Map sortedMap2 = new linkedHashMap<>();
//ASC
map.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
System.out.println(sortedMap);
//DESC Collections.reverseOrder || reversed()
map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.forEachOrdered(x -> sortedMap2.put(x.getKey(), x.getValue()));
// map.entrySet().stream()
// .sorted(Map.Entry.comparingByValue().reversed())
// .forEachOrdered(x -> sortedMap2.put(x.getKey(), x.getValue()));
System.out.println(sortedMap2);
//Collectors.toMap 直接返回排好序的map
map = map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue(), (x1, x2) -> x2, linkedHashMap::new));
// map = map.entrySet().stream()
// .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
// .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x1, x2) -> x2, linkedHashMap::new));
System.out.println(map);
}
}
{A=3, B=5, C=1, D=1, E=9}
{C=1, D=1, A=3, B=5, E=9}
{E=9, B=5, A=3, C=1, D=1}
{E=9, B=5, A=3, C=1, D=1}
补充知识:对Java8-2-Lambda表达式实战-一句话实现Map中按照Value排序
今天我们来实战一把, 对Map的Value值排序进行简化.
在以前的思路我们的做法如下:
public static> Map sortByValue(Map map) { List > list = new linkedList<>(map.entrySet()); Collections.sort(list, new Comparator >() { @Override public int compare(Map.Entry o1, Map.Entry o2) { return (o2.getValue()).compareTo(o1.getValue()); } }); Map result = new linkedHashMap<>(); for (Map.Entry entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; }
什么意思呢?意思就是先把Map变成可排序的List使用Comparator接口对entry进行排序, 可是这样代码很多很乱, 我们需要做一些简化.
第一步: 使用Lambda表达式先对Comparator接口做简化, 代码会变成如下情况:
public static> Map sortByValue(Map map) { List > list = new linkedList<>(map.entrySet()); list.sort(Comparator.comparing(Entry::getValue)); Map result = new linkedHashMap<>(); for (Map.Entry entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; }
这样的话, 一行代码就代替了五行, 但是会有个问题, 这样写只能从小到大排序很不灵活, 我们还有其他办法.来看下面的代码:
public static> Map sortByValue(Map map) { List > list = new linkedList<>(map.entrySet()); list.sort((o1, o2)-> o2.getValue().compareTo(o1.getValue())); Map result = new linkedHashMap<>(); for (Map.Entry entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; }
用lambda表达式就可以做到变换排序的方式, 只要改变o1和o2的顺序就可以了.哎, 可以还是很长, 我还想再少几句代码, 怎么办?
我们来分析下最原始的排序代码 ---> 首先是将Map转化为List
public static> Map sortByValue(Map map) { List > list = new linkedList<>(map.entrySet()); list.sort((o1, o2)-> o2.getValue().compareTo(o1.getValue())); Map result = new linkedHashMap<>(); list.stream().forEach(entry -> result.put(entry.getKey(), entry.getValue())); return result; }
也许做到上面这一步已经很满足了, 可是作为一个优秀的开发人员怎么能满足于这种程度, 我们要用两句话完成上面的功能.我们可以发现entrySet()是个集合, stream是有sort方法的, 可以set变成stream然后sort之后forEach到新的Map中, 牛逼吧, 废话少说,看代码.
public static> Map sortByValue(Map map) { Map sortMap = new linkedHashMap<>(); new map.entrySet().stream() .sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue())) .forEach(entry -> sortMap.put(entry.getKey(), entry.getValue())); return sortMap; }
高级程序员到这里就可以了, 下面提供一个工具类给大家使用.
public static> Map sortByValue(Map map, int flag) { Map sortMap = new linkedHashMap<>(); if(flag == 1) { map.entrySet().stream() .sorted((o1, o2) -> o1.getValue().compareTo(o2.getValue())) .forEach(entry -> sortMap.put(entry.getKey(), entry.getValue())); } else { map.entrySet().stream() .sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue())) .forEach(entry -> sortMap.put(entry.getKey(), entry.getValue())); } return sortMap; }
以上的代码已经够简洁了, 但是有一个中间变量, 我作为一个究极程序员是看不惯的, 能不能把它也省略掉一句代码实现整个功能呢? 答案是可以的.
public static> Map sortByValue2(Map map, int flag) { if(flag == 1) { return map.entrySet().stream().sorted((o1, o2) -> o1.getValue().compareTo(o2.getValue())).map(entry -> { Map result = new linkedHashMap<>(); result.put(entry.getKey(), entry.getValue()); return result; }).reduce((map1, map2) -> { map2.entrySet().forEach(entry -> map1.put(entry.getKey(), entry.getValue())); return map1; }).get(); } else { return map.entrySet().stream().sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue())).map(entry -> { Map result = new linkedHashMap<>(); result.put(entry.getKey(), entry.getValue()); return result; }).reduce((map1, map2) -> { map2.entrySet().forEach(entry -> map1.put(entry.getKey(), entry.getValue())); return map1; }).get(); }
思路是做好排序后将排序后的entry加入到新的Map里面, 再将stream
以上这篇java8 实现map以value值排序操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。



