//比较int类型
list.sort(Comparator.comparingInt(String::length)
.thenComparing(String::toLowerCase, Comparator.reverseOrder()));
System.out.println(list);
list.sort(Comparator.comparingInt(String::length).reversed()
.thenComparing(Comparator.comparing(String::toLowerCase, Comparator.reverseOrder())));
//比较时间等,stream流的sorted比较方法,里面传比较器就能实现比较
list.sorted(Comparator.comparing(Entity::getSort, Comparator.reverseOrder()))
//万能比较方法
public void testCompareTo(){
BigDecimal fullAmountDiscount = new BigDecimal("66.22");
/**
* param1;传的参数
* param2:要比较的参数
* param3:比较器
* 记法: 最后的0不变,比较符号往前移,(param1 > param2)
* (param1 < param2)
*/
Boolean dZero = Objects.compare(fullAmountDiscount, BigDecimal.ZERO, BigDecimal::compareTo) > 0;
Boolean xTen = Objects.compare(fullAmountDiscount, BigDecimal.TEN, BigDecimal::compareTo) < 0;
if (dZero && xTen){
System.out.println("比较的值合法的进来"+fullAmountDiscount);
}
}
//排序后再排序
List<GoodsSkuDTO> collect = item.getGoodsSkus().stream().sorted(Comparator.comparing(GoodsSkuDTO::getSerialNumber, Comparator.reverseOrder()).thenComparing(GoodsSkuDTO::getCreateTime,Comparator.reverseOrder())).collect(Collectors.toList());
Comparator比较器
最新推荐文章于 2023-11-08 00:00:07 发布