java8-stream常用


按条件查询单个实体类:

User u= user.stream().filter(item ->item.getName().equals(“zs”)).findAny().orElse(null);

按条件筛选多个实体类:

List u= user.stream().filter(item ->item.getName().equals(“zs”)).collect(Collectors.toList());

将list按对象中的某一属性分组:

Map> collect = list.stream().collect(Collectors.groupingBy(User::getUserNo));

将list聚合成key-对象中的某一属性,value-对象:

Map collect = list.stream().collect(Collectors.toMap(User::getUserNo, Function.identity()));

list中对象的某个属性累加:

Integer total = list.stream().mapToInt(User::getCount).sum();

 list中对象的某个属性聚合成list:

list list1 = list.stream().map(t -> t.getRole()).collect(Collectors.toList())

list快速遍历筛选:

long count = numbers.stream().filter(i->i>20).count();
List<Integer> list1 = list.stream().filter(i->i>50).collect(Collectors.toList());

list中求对象中某个属性的最大值:

Optional max = users.stream().max((p1, p2) -> Integer.compare(p1.getAge(), p2.getAge()));

 bigdecimal累计求和:

BigDecimal reduce = list.stream().map(ApAccountBegin::getBeginQuantity).reduce(BigDecimal.ZERO,BigDecimal::add);

筛选list中是否存在符合条件的字段:

boolean hasGrade = betweenList.stream().allMatch(t -> t.getGrade() == null);