去重
List<User> userList 和 List<Person> personList
List<User> userList = userList.stream()
.filter(user -> !personList.stream().map(person - >person.getId()).collect(Collectors.toList())
.contains(user.getId()))
.collect(Collectors.toList());
// 单条件去重
list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
-> new TreeSet<>(Comparator.comparing(user -> user .getId()))), ArrayList::new));
// 多条件升级
list.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(user -> user.getAge() + ";" + user.getName()))), ArrayList::new))
List<String> intersection = list1.stream()
.filter(item -> list2.contains(item))
.collect(toList());
List<String> reduce1 = list1.stream()
.filter(item -> !list2.contains(item))
.collect(toList());
List<String> reduce2 = list2.stream()
.filter(item -> !list1.contains(item))
.collect(toList());
List<String> listAllDistinct = listAll.stream()
.distinct()
.collect(toList());
排序
List<Person> sortedList = list.stream()
.sorted((o1,o2) -> o1.getAge() - o2.getAge())
.collect(Collectors.toList());
List<Person> sortedList = list.stream()
.sorted(Comparator.comparing(ClassImagePraise::getSort))
.collect(Collectors.toList());
List<Person> sortedList = list.stream()
.sorted(Comparator.comparing(ClassImagePraise::getSort)).reversed())
.collect(Collectors.toList());
list.stream()
.sorted(Comparator.comparing(Message::getMsg)
.thenComparing(Message::getId))
.collect(Collectors.toList());
list.stream()
.sorted(Comparator.comparing(Message::getMsg)
.thenComparing(Comparator.comparing(Message::getId).reversed()))
.collect(Collectors.toList());
list.stream()
.sorted(Comparator.comparing(Message::getMsg)
.thenComparing(Message::getId).reversed())
.collect(Collectors.toList());
list.stream()
.sorted(Comparator.comparing(Message::getMsg).reversed()
.thenComparing(Message::getId))
.collect(Collectors.toList());
过滤
List<Person> filterList = list.stream()
.filter(item -> item.getAge() > 3)
.collect(Collectors.toList());
筛选、转换
List<String> mapList1 = list.stream()
.map(Person::getName)
.collect(Collectors.toList());
List<String> mapList2 = list.stream()
.map(item -> item.getName())
.collect(Collectors.toList());
List<String> teacherIds = pList.stream()
.filter(s -> s.getId().equals("333"))
.map(p -> p.getUserId())
.collect(Collectors.toList());
统计
double sum = list.stream().mapToDouble(Person::getAge).sum();
Optional<Message> maxMassage = list.stream()
.collect(Collectors.maxBy(Comparator.comparing(Message::getId)));
Long maxId = maxMassage.get().getId();
LongSummaryStatistics lss = list.stream()
.collect(Collectors.summarizingLong(Message::getId));
分组
Map<Integer, List<Person>> map = list.stream()
.collect(Collectors.groupingBy(Person::getAge));
Map<String, Map<Integer, List<Person>>> map2 = list.stream()
.collect(Collectors.groupingBy(t->t.getName(),Collectors.groupingBy(t->t.getAge())));
Map<String, Map<Integer, LongSummaryStatistics>> map3 = list.stream()
.collect(Collectors.groupingBy(t->t.getName(),Collectors.groupingBy(t->t.getAge(),Collectors.summarizingLong(Person::getSize))));
遍历
items.forEach(item->{
System.out.println(item);
});