java8 stream map根据value排序或key排序(重要的)


根据value排序

List<String> items = Arrays.asList("apple", "apple", "banana", "apple", "orange", "banana", "papaya"); Map<String, Long> result = items.stream().collect( Collectors.groupingBy( Function.identity(), Collectors.counting() ) ); Map<String, Long> finalMap = new LinkedHashMap<>(); //Sort a map and add to finalMap result.entrySet().stream() .sorted(Map.Entry.<String, Long>comparingByValue() .reversed()).forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue())); System.out.println(finalMap);
<span style="color:#999999">根据key排序</span>

 
Map<String,String> result=new HashMap<>(); result.put("13", "derek"); result.put("37","dad"); result.put("22","mom");  Map<String, String> finalMap = new LinkedHashMap<>();  //Sort a map and add to finalMap result.entrySet().stream() .sorted(Map.Entry.<String, String>comparingByKey() .reversed()).forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));  System.out.println(finalMap);

dubbo技术学习,更多知识请访问https://www.itkc8.com


 

 


 
升序
Map<String, String> finalMap = new TreeMap<>();

 
    原文作者:最是那一低头的温柔
    原文地址: https://blog.csdn.net/HUXU981598436/article/details/78504766
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞