java8 的stream map 排序 记录下

Map keywordTimes = new HashMap<>();

keywordTimes.put(1,2);

keywordTimes.put(2,3);

keywordTimes.put(4,5);

keywordTimes.put(3,4);

//返回匹配度最高的即 关键词匹配最多的 先按升序排序
Map sortMap = keywordTimes
        .entrySet()
        .stream()
        .sorted(Collections.reverseOrder(comparingByValue()))
        .collect(
                Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1,e2)->e2,
                        LinkedHashMap::new)
        );

//降序

Map sortMap = keywordTimes .entrySet() .stream() .sorted(comparingByValue()) .collect( Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1,e2)->e2, LinkedHashMap::new) );

你可能感兴趣的:(java)