A. Flow is not for all the elements for the first time operation and then for the second operation, but for an element to perform all the operations, all operations are short circuit operation with Java && | | operators are consistent
Pseudo code:
list.stream().mapToInt(item -> {int lengt = item.length(); System.out.println(item);
return length; }).filter(length -> length == 5).findFirst().isPresent(System.out : println)
FlatMap merges elements of multiple streams into a single stream
Pseudo code:
list.stream().map(item -> item.spilt("")).flatMap(Arrays::stream).distinct().collect(Collectors.toList())Copy the code
Pseudo code:
List<String> reuslt = list.stream().flatMap.flatMap(item->list2.stream().map(item2->item+""+item2)). Collect (collectors. ToList ());Copy the code
Grouping and partitioning
GroupingBy (student ::getName)) to group students by name, similar to group by in an SQL statement
Grouping:
Map<String,List<Student>> map = students.stream().collect(Collectors.groupingBy(Student::getName));
Map<Integer,List<Student>>map = students.stream().collect(Collectors.groupingBy(Student::getScore)); Map<String,Long>map = student.stream().collect(Collectors.groupingBy(Student::getName,Collectors.counting());
Map<String,Double>map = students.stream().collect(Collectors.groupingBy(Student::getName,Collectors.averagingDouble(Student::getScore)));
Multi-level grouping:
Map<Integer,Map<String,List<Student>>> map = students.Stream().collect(groupingBy(Student::getScore,groupinBy(Student::getName)))
Partitions:
Map<Boolean,List<Student>> map = students.stream().collect(Collectors.partitioningBy(student->student.getScore()>=90));
Multi-level partitioning:
Map<Boolean,Map<Boolean,List<Student>>> map = students.stream().collect(partitioningBy(student -> student.getScore()>80,partitionBy(student -> student.getScore()>90)));Copy the code
The more specific the JDK implementation is, the better. For example, the size of a collection created by student can be calculated directly using the.size() method. However, the JDK provides many implementations, where the more specific the method, the more extendable it is