jdk1.8 集合
作者:互联网
//差集(从旧对象中找出新对象没有的数据,即删除)
List delPaylist = oldList.stream()
.filter(item -> !newList.stream().map(e -> e.getId()).collect(Collectors.toList()).contains(item.getId()))
.collect(Collectors.toList());
//取出某个属性值封装集合
userList=list.stream().map(UserConversation::getConId).collect(Collectors.toList());
//int、double、long: 求和
double max = list.stream().mapToDouble(User::getHeight).sum();
//bigdecimal求和
shopOrderOtherFeeList.stream().map(ShopOrderOtherFee::getFeeAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//过滤
resultList.stream().filter(a ->a.getApproveSts() !=null&&
(a.getApproveSts().equals(“1”)||a.getApproveSts().equals(“3”))).collect(Collectors.toList());
//排序
// 正序 写法1
list.sort(Comparator.comparing(Integer::intValue));
System.out.println("=正序 写法 1");
list.forEach(System.out::println);
// 正序 写法2(naturalOrder 自然排序就是正序)
// Comparator.comparing 与Comparator.comparingInt,两者可以互相代替
list.sort(Comparator.comparing(Integer::intValue, Comparator.naturalOrder()));
System.out.println("=======正序 写法 2======");
list.forEach(System.out::println);
// 倒序 reverseOrder倒序
// Comparator.comparing 与Comparator.comparingInt,两者不可以互相代替
list.sort(Comparator.comparing(Integer::intValue, Comparator.reverseOrder()));
System.out.println("=======倒序======");
list.forEach(System.out::println);
通过指定字段分组Collectors.groupingBy().
Map<String, List> groupByStatus = userList.stream().collect(Collectors.groupingBy(User::getStatus));
//去重distinct().
List userIdList = userList.stream().distinct().collect(Collectors.toList());
//单字段多字段排序sort().
userList.sort(Comparator.comparing(User::getUserId));
userList.sort(Comparator.comparing(User::getUserId).thenComparing(User::getStatus));
标签:jdk1.8,stream,Collectors,collect,list,comparing,集合,Comparator 来源: https://blog.csdn.net/weixin_45281605/article/details/121136883