-
list
- 升序
//第一种 list.sort((a,b)->a.getSeqNo().compareTo(b.getSeqNo())); //第二种 list.sort(Comparator.comparing(TransportationPriceAdvance::getFristWeight)); //第三种 ==》排序后返回一个新的list List<Object> specialProducts =list.stream().sorted((u1,u2)->u1.getSpecialProduct().compareTo(u2.getSpecialProduct())).collect(Collectors.toList());
- 降序
//第一种 entry.getValue().sort((a,b)->b.getSeqNo().compareTo(a.getSeqNo())); //第二种 list.sort(Comparator.comparing(TransportationPriceAdvance::getFristWeight).reversed()); //第三种 ==》排序后返回一个新的list List<Object> specialProducts =list.stream().sorted((u1,u2)->u2.getSpecialProduct().compareTo(u1.getSpecialProduct())).collect(Collectors.toList());
- 升序
-
map
comparingByValue().reversed倒序,不指定类型,会默认为<java.util.Map.Entry<java.lang.Object,V>>
- 升序
map.entrySet().stream() .sorted(Map.Entry.<String, Integer>comparingByValue()) .forEachOrdered(x -> result.put(x.getKey(), x.getValue()));
- 降序
map.entrySet().stream() .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()) .forEachOrdered(x -> result.put(x.getKey(), x.getValue()));
- 升序
java8-排序
最新推荐文章于 2024-07-24 17:49:50 发布