根据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<>();