List<Student> above90List = students.stream()
.filter(t->t.getScore()>90)
.collect(Collectors.toList());
List<String> nameList = students.stream()
.map(Student::getName)
.collect(Collectors.toList());
List<String> above90Names = students.stream()
.filter(t->t.getScore()>90)
.map(Student::getName)
.collect(Collectors.toList());
List<String> list = Arrays.asList(new String[]{"abc", "def", "hello", "Abc"});
List<String> retList = list.stream()
.filter(s->s.length()<=3)
.map(String::toLowerCase)
.distinct()
.collect(Collectors.toList());
//返回 对象集合以类属性一升序排序
list.stream().sorted(Comparator.comparing(类::属性一));
//返回 对象集合以类属性一降序排序 注意两种写法
list.stream().sorted(Comparator.comparing(类::属性一).reversed());//先以属性一升序,结果进行属性一降序
list.stream().sorted(Comparator.comparing(类::属性一,Comparator.reverseOrder()));//以属性一降序
//返回 对象集合以类属性一升序 属性二升序
list.stream().sorted(Comparator.comparing(类::属性一).thenComparing(类::属性二));
//返回 对象集合以类属性一降序 属性二升序 注意两种写法
list.stream().sorted(Comparator.comparing(类::属性一).reversed().thenComparing(类::属性二));//先以属性一升序,升序结果进行属性一降序,再进行属性二升序
list.stream().sorted(Comparator.comparing(类::属性一,Comparator.reverseOrder()).thenComparing(类::属性二));//先以属性一降序,再进行属性二升序
//返回 对象集合以类属性一降序 属性二降序 注意两种写法
list.stream().sorted(Comparator.comparing(类::属性一).reversed().thenComparing(类::属性二,Comparator.reverseOrder()));//先以属性一升序,升序结果进行属性一降序,再进行属性二降序
list.stream().sorted(Comparator.comparing(类::属性一,Comparator.reverseOrder()).thenComparing(类::属性二,Comparator.reverseOrder()));//先以属性一降序,再进行属性二降序
//返回 对象集合以类属性一升序 属性二降序 注意两种写法
list.stream().sorted(Comparator.comparing(类::属性一).reversed().thenComparing(类::属性二).reversed());//先以属性一升序,升序结果进行属性一降序,再进行属性二升序,结果进行属性一降序属性二降序
list.stream().sorted(Comparator.comparing(类::属性一).thenComparing(类::属性二,Comparator.reverseOrder()));//先以属性一升序,再进行属性二降序
通过以上例子我们可以发现
Comparator.comparing(类::属性一).reversed();
Comparator.comparing(类::属性一,Comparator.reverseOrder());
两种排序是完全不一样的,一定要区分开来:1是得到排序结果后再排序,2是直接进行排序,很多人会混淆导致理解出错,2更好理解,建议使用2
Collections.sort(orgDataList,(o1,o2)->{
String order1=o1.getSortOrder();
if(order1==null){
order1="";
}
String order2=o2.getSortOrder();
if(order2==null){
order2="";
}
return order1.compareTo(order2);
});
eduDataInfo = eduDataInfo.stream()
.sorted(new Comparator<EduExperience>() {
@Override
public int compare(EduExperience o1, EduExperience o2) {
if (o1.getEducationCode().compareTo(o2.getEducationCode()) == 0 && o1.getEducationCode().compareTo(o2.getEducationCode()) == 0) {
return o1.getSeqNum().compareTo(o2.getSeqNum());
} else if (o1.getEducationCode().compareTo(o2.getEducationCode()) == 0) {
return o2.getDegreeCode().compareTo(o1.getDegreeCode());
} else {
return o2.getEducationCode().compareTo(o1.getEducationCode());
}
}
}).collect(Collectors.toList());
List<Student> list = students.stream()
.sorted(Comparator.comparing(Student::getScore).reversed())
.skip(2).limit(3)
.collect(Collectors.toList());
double sum = students.stream().mapToDouble(Student::getScore).sum();
为避免装箱/拆箱,提高性能,Stream提供了返回基本类型特定流的方法。
Student student = students.stream().max(Comparator.comparing(Student::getGrade)).get();
或
Student student = students.stream().min(Comparator.comparing(Student::getGrade).reversed()).get();
这里假定students不为空
long above90Count = students.stream().filter(t->t.getScore()>90).count();
boolean allPass = students.stream().allMatch(t->t.getScore()>=60);
如果流为空,那么这几个函数的返回值都是true。
students.stream().filter(t->t.getScore()>90).forEach(System.out::println);
Student[] above90Arr = students.stream().filter(t->t.getScore()>90).toArray(Student[]::new);
toSet的使用与toList类似,只是它可以排重。toList背后的容器是ArrayList, toSet背后的容器是HashSet。
toMap将元素流转换为一个Map,我们知道,Map有键和值两部分,toMap至少需要两个函数参数,一个将元素转换为键,另一个将元素转换为值。
Map<String, Double> nameScoreMap = students.stream().collect(Collectors.toMap(Student::getName, Student::getScore, (oldValue, value)->value));
Map<String, Student> byIdMap = students.stream().collect(Collectors.toMap(Student::getId, Function.identity(), (oldValue, value)->value));
Map<String, Integer> strLenMap = Stream.of("abc", "hello", "abc").collect(Collectors.toMap(Function.identity(), t->t.length(), (oldValue, value)->value));
分组类似于数据库查询语言SQL中的group by语句,它将元素流中的每个元素分到一个组,可以针对分组再进行处理和收集。
Map<String, List<Student>> groups = students.stream().collect(Collectors.groupingBy(Student::getGrade));
Map<String, Long> gradeCountMap = students.stream().collect(groupingBy(Student::getGrade, counting()));
Map<String, Long> wordCountMap = Stream.of("hello", "world", "abc", "hello").collect( groupingBy(Function.identity(), LinkedHashMap::new, counting()));
Map<String, List<String>> gradeNameMap = students.stream().collect(groupingBy(Student::getGrade, mapping(Student::getName, toList())));
Map<String, List<Student>> gradeStudentMap = students.stream().collect(groupingBy(Student::getGrade, collectingAndSort(toList(), Comparator.comparing(Student::getScore).reversed())));
Map<String, List<Student>> gradeStudentMap = students.stream()
.collect(groupingBy(Student::getGrade, collectingAndFilter(toList(), t->t.getScore()<60)));