获取List集合中的某一种属性
List<Long> communityIds = equipmentList.stream()
.map(Equipment::getCommunityId)
.collect(Collectors.toList());
由list生成map信息
Map communityMap = communityResponseList.stream()
.collect(Collectors.toMap(CommunityResponse::getId, e -> e));
List排序
list.stream().sorted();
list.stream().sorted(Comparator.reverseOrder());
list.stream().sorted(Comparator.comparing(Student::getAge));
list.stream().sorted(Comparator.comparing(Student::getAge).reversed);
List去重
List<String> stringList = new ArrayList<String>();
...
stringList = stringList.stream().distinct().collect(Collectors.toList());
studentList = studentList.stream().distinct().collect(Collectors.toList());
studentList = studentList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new));
private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
};
studentList = studentList.stream().filter(distinctByKey(Student::getName)).collect(Collectors.toList());