lambda常用表达式

lambda常用表达式

list整形字段求和:

list.stream().mapToInt(E::getValue()).sum()

list金额字段求和:

list.stream().map(Goods::getAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);

对象list转idList:

List<Long> idList= studentList.stream().map(Student::getId).collect(Collectors.toList());

统计集合中符合条件的数据数量:

long count = list.stream().filter(s -> s.getId() == 1).count();

统计集合中符合条件的数据:

List<Student> list = studentList.stream()
.filter(student -> student.getAge() == 12).collect(Collectors.toList());

两个list去重:

List<String> collect = Stream.of(list,list1)
.flatMap(Collection::stream).distinct().collect(Collectors.toList());

list字段排序:

List<Student> list = studentList.stream()
.sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());

list过滤后排序再取值:

List<String> orderProductList = studentList.stream()
.filter(student-> student.getAge().equals(20))
.sorted(Comparator.comparing(Student::getId)
.reversed()).map(student-> student.getId())
.collect(Collectors.toList());

list分组:

Map<Long, List<Student>> mapByOne = studentList.stream().collect(Collectors.groupingBy(Student::getId));

list删除:

list.removeIf(item -> item.getId().equals(studentGroup.getId()));

你可能感兴趣的:(后端,后端)