排序 草稿

//        List<Map.Entry<String,Long>> lstEntry = new ArrayList<>(collect.entrySet());
// Collections.sort(lstEntry,((o1, o2) -> {
// return o1.getValue().compareTo(o2.getValue());
// }));



//这里将map.entrySet()转换成list,再用collections工具类中的sort()方法完成排序操作
List<Map.Entry<String, Long>> list = new ArrayList<>(collect.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Long>>(){
@Override
public int compare(Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) {
int t = - o1.getValue().compareTo(o2.getValue());
if(t == 0)
return o1.getKey().compareTo(o2.getKey());
return t;
}

// @Override
// public int compare(Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) {
// return - o1.getValue().compareTo(o2.getValue());
// }
});

??
Stream<Map.Entry<String, Long>> limit = list.stream().limit(10);


posted on 2022-11-20 10:50  daofree  阅读(13)  评论(0编辑  收藏  举报