Java8 使用 stream().sorted()对List集合进行排序

public static void main(String[] args){
TGoodsInfo tGoodsInfo1 = new TGoodsInfo();
tGoodsInfo1.setExpendIntegral(0);//积分大小
tGoodsInfo1.setUpdatedTime(DateUtil.getHalfYearStartTime(new Date()));//时间
TGoodsInfo tGoodsInfo2 = new TGoodsInfo();
tGoodsInfo2.setExpendIntegral(3);
tGoodsInfo2.setUpdatedTime(new Date());
TGoodsInfo tGoodsInfo3 = new TGoodsInfo();
tGoodsInfo3.setExpendIntegral(3);
tGoodsInfo3.setUpdatedTime(DateUtil.getHalfYearEndTime(new Date()));
TGoodsInfo tGoodsInfo4 = new TGoodsInfo();
tGoodsInfo4.setExpendIntegral(4);
tGoodsInfo4.setUpdatedTime(DateUtil.getHalfYearEndTime(new Date()));
List<TGoodsInfo> result = new ArrayList <>();
result.add(tGoodsInfo4);
result.add(tGoodsInfo1);
result.add(tGoodsInfo2);
result.add(tGoodsInfo3);

  • 按照 积分和时间倒排序

result = result.stream().sorted(
Comparator.comparing(TGoodsInfo::getExpendIntegral).
thenComparing(TGoodsInfo::getUpdatedTime).reversed())
.collect(Collectors.toList());
for (TGoodsInfo tGoodsInfo : result) {
System.out.println(tGoodsInfo);
}

  • 按照 积分和时间正排序

result = result.stream().sorted(
Comparator.comparing(TGoodsInfo::getExpendIntegral).
thenComparing(TGoodsInfo::getUpdatedTime))
.collect(Collectors.toList());
for (TGoodsInfo tGoodsInfo : result) {
System.out.println(tGoodsInfo);
}

  • 按照 积分正排序和时间倒排序

result = result.stream().sorted(
Comparator.comparing(TGoodsInfo::getExpendIntegral).reversed().
thenComparing(TGoodsInfo::getUpdatedTime).reversed())
.collect(Collectors.toList());
for (TGoodsInfo tGoodsInfo : result) {
System.out.println(tGoodsInfo);
}

  • 按照 积分到排序和时间正排序

result = result.stream().sorted(
Comparator.comparing(TGoodsInfo::getExpendIntegral).reversed())
.collect(Collectors.toList());
for (TGoodsInfo tGoodsInfo : result) {
System.out.println(tGoodsInfo);
}

  • 按照积分到排序

result = result.stream().sorted(
Comparator.comparing(TGoodsInfo::getExpendIntegral).reversed())
.collect(Collectors.toList());
for (TGoodsInfo tGoodsInfo : result) {
System.out.println(tGoodsInfo);
}

  • 按照积分正排序

result = result.stream().sorted(
Comparator.comparing(TGoodsInfo::getExpendIntegral))
.collect(Collectors.toList());
for (TGoodsInfo tGoodsInfo : result) {
System.out.println(tGoodsInfo);
}
}

Java8 使用 stream().sorted()对List集合进行排序

上一篇:react setState


下一篇:文本比较组件DiffPlex