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<Student> list = students.stream()
.filter(t->t.getScore()>90)
.sorted(Comparator.comparing(Student::getScore).reversed().thenComparing(Student::getName))
.collect(Collectors.toList());
List<Student> list = students.stream()
.filter(t->t.getScore()>90)
.sorted(Comparator.comparing(Student::getScore).thenComparing(Student::getName)).reversed()
.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)));