The newly added Stream API in Java 8 really introduces the functional programming style into Java, which is somewhat similar to the syntax of Mongodb
That is, the data of the operation can be regarded as a stream, the stream is transmitted in the pipeline, processed on the node, filtered, sorted, aggregated and so on.
Data source: The source of the stream, which can be a collection (List, Set), array, I/O, generator, etc.
Aggregation operations: some operations similar to SQL, such as: filter, map, find, sorted, match, etc.
Two important features of Stream:
1.Pipeling 中间操作都会返回流对象本身。这样多个操作串联成一个管道,如同流式风格
2.内部迭代:以前集合遍历都是Iterator或者Foreach等方式进行的外部迭代,Stream流提供了内部迭代的方式,通过访问者模式实现。
Creation of Stream stream: There are two ways to create stream stream:
1.stream() 为集合创建串行流
2.parallelStream() 为集合创建并行流
forEach iterates:
List<String> list = Arrays.asList("mysql", "java8", "tomcat", "spring", "mybatis", "springBoot");
list.stream().forEach(System.out::println);
遍历list的内容并进行打印
filter filters the elements of the stream by the filter conditions
List<String> list = Arrays.asList("mysql", "java8", "tomcat", "spring", "mybatis", "springBoot");
List<String> list = list.stream().filter(string->string.endsWith("s")).collect(Collectors.toList());
过滤以s结尾的元素
int count = list.stream().filter(string->string.endsWith("s")).collect(Collectors.toList()).count();
获取以s结尾元素的数量
map is used to map to the result corresponding to each element
List<Integer> intList = Arrays.asList(12, 11, 20, 20, 20, 20, 4, 9, 99, 35);
List<Integer> list = intList.stream().map(i -> i + 10).distinct().collect(Collectors.toList());
对集合中每个元素进行+10操作
limit specifies the number of returned results
List<Integer> intList = Arrays.asList(12, 11, 20, 20, 20, 20, 4, 9, 99, 35);
List<Integer> list = intList.stream()limit(5).map(i -> i + 10).distinct().collect(Collectors.toList());
对集合每个元素+10操作,返回5条
sorted sorts the processed elements
List<Integer> intList = Arrays.asList(120, 11, 20, 5, 20, 20, 4, 9, 99, 35);
List<Integer> list = intList.stream()limit(5).sorted().map(i -> i + 10).distinct().collect(Collectors.toList());
对集合每个元素+10操作,返回5条,并且返回的结果进行排序
parallelStream parallel processing
List<String> list1 = Arrays.asList("2","","333","","","323","fa");
long emptyCount = list1.parallelStream().filter(i->i.isEmpty()).count();
获取空字符串的数量
Collectors implement many reduction operations, such as converting streams to sets or strings, etc.
List<String> list = list.stream().filter(string->string.endsWith("s")).collect(Collectors.toList());
将流转化为List
String result = list.stream().limit(5).sorted().filter(i -> i.length() > 5).collect(Collectors.joining(", "));
将流转化为字符串
Statistics are generally used for numerical types such as int double long float and other elements in the collection to perform statistics on them.
List<Integer> intList = Arrays.asList(12, 11, 20, 20, 20, 20, 4, 9, 99, 35);
IntSummaryStatistics sum = intList.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println(sum);
System.out.println(sum.getMax());
System.out.println(sum.getAverage());
System.out.println(sum.getMin());
System.out.println(sum.getCount());
Example of use:
List<ChapterEntity> chapterList = chapterMapper.findList(subjectId);
RelationKnowledge condition = new RelationKnowledge(knowledgeId, subjectId, knowledgeName);
List<RelationKnowledge> list = relationMapper.getKnowledgeListBySubject(condition);
chapterList.stream().forEach(chapter -> {
List<RelationKnowledge> tempList = list.stream().filter(relationKnowledge ->
chapter.getId().equals(relationKnowledge.getChapterId())
).collect(Collectors.toList());
chapter.setRelationKnowledgeList(tempList);
});