The article directories

  • preface

  • The Stream feature

  • Create Stream

  • 2.1 Creating streams with collections

  • 2.2 Creating streams with arrays

  • 2.3 Stream Static method

  • Use cases of Stream

  • 3.1 traversal forEach

  • 3.2 filter filter

  • 3.3 Finding findFirst, findAny

  • 3.4 match the match

  • 3.5 map a map

  • 3.6 Intercepting a Flow Limit

  • 3.7 sorting sorted

  • 3.8 去重 distinct

  • 3.9 statistical summaryStatistics

  • 3.10 reduce the reduce

  • 3.11 Collect toList, toSet, and toMap

  • 3.12 Group partitioningBy and groupingBy

  • 3.13 combination of joining

  • Fourth, demo code acquisition

preface

Java 8 (also known as JDK 1.8) is a major release of Java language development. Oracle released Java 8 on March 18, 2014. It supports functional programming, a new JavaScript engine, a new date API, a new Stream API, and more. The newly added Stream API (java.util.stream) brings a true functional programming style to Java. It lets you process data in a declarative way, resulting in efficient, clean, and concise code. This style treats the collection of elements to be processed as a stream that travels through a pipe and can be processed at the nodes of the pipe, such as filtering, sorting, aggregating, and so on.

The Stream feature

  1. Elements are objects of a specific type that form a queue. A Stream in Java does not store elements, but evaluates on demand. It evaluates data according to specific rules and generally outputs results.
  2. Stream does not change the data source, and generally produces a new collection or value.
  3. Stream Can be a collection, an array, an I/O channel, a generator, etc.
  4. Stream has a delayed execution property, where intermediate operations are executed only when terminal operations are called. All intermediate operations return the stream object itself. These operations can be cascaded into a pipe, as in fluent style.
  5. In the past, collections were iterated explicitly outside the collection by Iterator or for-each, which is called external iteration. A Stream provides a means of iterating internally through a Visitor pattern.

The operations on a Stream can be roughly divided into two types:

  1. Intermediate operations: Each operation returns the stream object itself.

*** Terminal operation: A stream can perform only one terminal operation, that is, generate a new set or a new value. The stream cannot be used again after the terminal operation. **

Create Stream

In Java 8, streams can be created from collections or arrays, generating streams of two types:

Stream () : a serial stream that is operated sequentially by the main thread. ParallelStream () : Parallel streams that operate internally in parallel with multiple threads, but only if there is no sequential requirement for data processing in the stream. For example, calculating the sum of quantities in a set. If there is a large amount of stream data, parallel stream can speed up processing. Serial streams can replace sequential streams with parallel streams using the parallel() method.

2.1 Creating streams with collections

Because collections inherit or implement the java.util.Collection interface, which defines the Stream() and parallelStream() methods, streams can be created from the stream() and parallelStream() methods of collections.

List<String> List = array. asList(" array ", "Array "," array "); // Create a serial Stream<String> Stream = list.stream(); // create a parallelStream Stream<String> parallelStream = list.parallelstream (); 123456Copy the code

2.2 Creating streams with arrays

Stream (T[] array) creates streams from Arrays using the java.util.array.stream (T[] array) method.

[] persons = {" persons ", "persons "," persons "}; Stream<String> Stream = array. Stream (persons); Stream<String> parallelStream = array.stream (persons).parallel(); 123456Copy the code

2.3 Stream Static method

Generate streams using static methods of Stream, such as of(), iterate(), generate(), and so on.

Stream<String> stream2 = stream. of(" 三", "三"," 三"); Stream<Integer> stream3 = stream. iterate(1, x -> x + 2). Limit (5); stream3.forEach(System.out::println); Stream<UUID> stream4 = stream. generate(UUID::randomUUID).limit(2); stream4.forEach(System.out::println); 123456789Copy the code

Use cases of Stream

All of the following cases will be based on student data, student categories, and test data as follows:

package com.nobody; /** * @description Student class * @author Mr. Nobody * @date 2021/1/17 * @version 1.0 */ public class Student {// primary key private String id; // Name private String name; // private int age; // private String sex; // private double score; public Student(String id, String name, int age, String sex, double score) { this.id = id; this.name = name; this.age = age; this.sex = sex; this.score = score; } // omit get and set methods, toString methods, If the test need to add 12345678910111213141516171819202122232425262728 List < Student > students = new ArrayList < > (16); Students. Add (new Student("1", "三", 18, "male", 88)); Students. Add (new Student("2", "三 ", 17, "male", 60)); Students. Add (new Student("3", "王五", 18, "male", 100)); Students. Add (new Student("4", "zhao 6 ", 20, "male", 10)); Add (new Student("5", "dong 7 ", 14, "female", 95)); Students. Add (new Student("6", "8 ", 21, "male", 55)); Add (new Student("7", "lao9 ", 20, "female", 66)); Add (new Student("8", "xiaoming ", 18, "male", 100)); Add (new Student("9", "小红", 22, "female", 95)); Students. Add (new Student("10", "小张", 25, "male", 90)); 1234567891011Copy the code

3.1 traversal forEach

students.stream().forEach(System.out::println); Student{id='2', name=' 3 ', age=18, sex=male, score=88.0} Student{id='2', name=' 4 ', age=17, sex=male, Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score = 90.0} 12345678910111213Copy the code

3.2 filter filter

List<Student> students1 = students.stream().filter(Student -> student.getScore() == 100).collect(Collectors.toList()); students1.forEach(System.out::println); Student{id='3', name=' 5 ', age=18, sex=male, score=100.0} Student{id='8', name=' 6 ', age=18, sex=male, Score = 100.0} 12345678Copy the code

3.3 Finding findFirst, findAny

Generally, filter and find are used together, that is, to obtain a piece of data from the matching data.

// Serial stream, StudentOptional = student.stream ().filter(Student -> student.getage () >= 20).findFirst(); if (studentOptional.isPresent()) { Student student = studentOptional.get(); System.out.println(student); } / / output statements can be abbreviated below/above/studentOptional ifPresent (System. Out: : println); // Parallel streams, match any one, FindAny (Optional<Student> studentOptiona2 = student.parallelStream ().filter(Student -> student.getage () >= 20).findAny(); studentOptiona2.ifPresent(System.out::println); Student{id=' 5 ', name=' 6 ', age=20, sex=male, score=10.0} Student{id='7', name=' 9 ', age=20, sex=female, Score = 66.0} 123456789101112131415161718Copy the code

3.4 match the match

Boolean anyMatch = students.stream().anymatch (student -> student.getScore() == 100); Boolean allMatch = students.stream(). AllMatch (student -> student.getScore() == 100); Boolean noneMatch = students.stream().nonematch (student -> student.getScore() == 100); System.out.println(anyMatch); System.out.println(allMatch); System.out.println(noneMatch); True false false 1234567891011121314Copy the code

3.5 map a map

Mapping, as the name implies, maps one object to another. That is, all elements in a Stream are mapped to another Stream according to certain mapping rules. There are two types of mapping: Map and flatMap.

Map: Takes as an argument a function that applies to each element in the Stream, forming a new element that forms a new Stream.

**

FlatMap: Takes as a parameter a function that converts each element in the stream to another stream and then joins all the streams to form a final stream.

**

List<String> studentNames = students.stream().map(Student::getName).collect(Collectors. ToList ()); System.out.println(studentNames); List<Double> score = student.stream ().map(student -> student.getScore() + 10) .collect(Collectors.toList()); System.out.println(studentScores); [98.0, 70.0, 110.0, 20.0, 105.0, 65.0, 76.0, 110.0, 105.0, 65.0, 76.0, 110.0, 105.0, 100.0] 123456789101112 List<String> List = Arrays. AsList (" A-b-c-d ", "G-h-i "); List<String> list1 = list.stream().flatMap(s -> Arrays.stream(s.split("-"))).collect(Collectors.toList()); System.out.println(list1); [a, b, C, D, g, h, I] 123456Copy the code

3.6 Intercepting a Flow Limit

The limit method is used to get a specified number of streams. List<Student> students2 = students.stream().filter(Student -> student.getscore () > 70) .limit(5).collect(Collectors.toList()); students2.forEach(System.out::println); // Skip the first one and get two more. List<Student> students8 = students.stream().skip(1).limit(2).collect(Collectors. ToList ()); Random Random = new Random(); random.ints().limit(5).sorted().forEach(System.out::println); Student{id=' 2 ', name=' 3', age=18, sex=male, score=88.0} Student{id='3', name=' 5 ', age=18, sex=male, Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score = 95.0} - 1490202714 145340547 368332155 388399398 1099579920 123456789101112131415161718192021Copy the code

3.7 sorting sorted

The sorted method is used to sort elements in a stream. There are two sorts:

Sorted () : Sort naturally. Elements in a stream need to implement the Comparable interface.

**

sorted(Comparator
comparator) : custom collator is required.

**

List<Student> students3 = students.stream().sorted(Comparator.comparing(Student::getScore)) .collect(Collectors.toList()); System.out.println(" in ascending order "); students3.forEach(System.out::println); // List<Student> students4 = students.stream(). Sorted (Comparator.comparing(Student::getScore).reversed()) .collect(Collectors.toList()); System.out.println(" in descending order "); students4.forEach(System.out::println); // In ascending order by grade, List<Student> students5 = students.stream() .sorted(Comparator.comparing(Student::getScore).thenComparing(Student::getAge)) .collect(Collectors.toList()); System.out.println(" ascending by grade, ascending by age "); students5.forEach(System.out::println); List<Student> students6 = students.stream().sorted((s1, s2) -> {if (s1.getscore ()! = s2.getScore()) { return (int) (s1.getScore() - s2.getScore()); } else { return (s2.getAge() - s1.getAge()); } }).collect(Collectors.toList()); System.out.println(" in ascending order by grade, in descending order by age "); students6.forEach(System.out::println); 123456789101112131415161718192021222324252627Copy the code

3.8 去重 distinct

List<String> list2 = Arrays.asList("a", "b", "a", "c", "f", "e", "f"); List<String> list3 = list2.stream().distinct().collect(Collectors.toList()); Set<String> stringSet = list2.stream().collect(Collectors.toSet()); System.out.println(list3); System.out.println(stringSet); [a, b, c, e, e] 123456789Copy the code

3.9 statistical summaryStatistics

Some Collectors can generate statistics. For example, Collectors provide a series of static methods for collecting statistics. These methods are mainly used for basic types such as int, double, and Long.

// summaryStatistics DoubleSummaryStatistics DoubleSummaryStatistics = students.stream().mapToDouble(Student::getScore).summaryStatistics(); System. The out. Println (" is mean: "+ doubleSummaryStatistics. GetAverage ()); System. The out. Println (" total number: "+ doubleSummaryStatistics. GetCount ()); System. The out. Println (" is the maximum: "+ doubleSummaryStatistics. GetMax ()); System. The out. Println (" is the minimum: "+ doubleSummaryStatistics. GetMin ()); System. The out. Println (" total value: "+ doubleSummaryStatistics. GetSum ()); // Average value of output results: 75.9 Total Numbers: 10 Max. : 100.0 Min. : 10.0 Total values: 759.0 123456789101112131415 // Count count long count = students.stream().count(); / / average Double averageScore = students. Stream () collect (Collectors. AveragingDouble Student: : getScore ()); // Optional<Double> maxScore = students.stream().map(Student::getScore).max(Double::compare); Optional<Double> minScore = students.stream().map(Student::getScore).min(Double::compare); SumScore = students.stream().mapTodouble (Student::getScore).sum(); // Count all DoubleSummaryStatistics doubleSummaryStatistics1 = students.stream().collect(Collectors.summarizingDouble(Student::getScore)); System.out.println(" single dimension calculation: "); System.out.println(" count: "+ count); System.out.println(" average: "+ averageScore); Maxscore.ifpresent (aDouble -> system.out.println (" Max: "+ aDouble)); Minscore.ifpresent (aDouble -> system.out.println (" min: "+ aDouble)); System.out.println(" sum: "+ sumScore); System.out.println(" Count all at once: "+ doubleSummaryStatistics1); / / the output a single dimension calculation: statistical number: 10 average: 75.9 maximum: minimum 100.0:10.0 summation: 759.0 12345678910111213141516171819202122232425262728Copy the code

3.10 reduce the reduce

Reduction, a stream reduced (reduced) into a value, can achieve the sum of sets, product and maximize operations.

List<Integer> integerList = Arrays.asList(6, 7, 1, 10, 11, 7, 13, 20); // Sum Optional<Integer> sum1 = integerlist.stream ().reduce(Integer::sum); Integer sum2 = integerlist.stream ().reduce(10, Integer::sum); Optional<Integer> max1 = integerlist.stream ().reduce((x, y) -> x > y? x : y); Integer max2 = integerlist.stream ().reduce(50, Integer:: Max); Optional<Integer> min = integerList.stream().reduce(Integer::min); // Product Optional<Integer> product = integerlist.stream ().reduce((x, y) -> x * y); System.out.println(" primitive set: "+ integerList); System.out.println(" sum1: "+ sum1. Get () + "," + sum2); System.out.println(" set Max: "+ max1.get() + "," + max2); System.out.println(" set min: "+ min.get()); System.out.println(" product: "+ product.get()); [6, 7, 1, 10, 11, 7, 13, 20] aggregate: 75,85 aggregate: 20,50 aggregate: 1 aggregate: 1 8408400, 123456789101112131415161718192021222324,Copy the code

3.11 Collect toList, toSet, and toMap

A Stream in Java does not store elements, but evaluates on demand. It evaluates data according to specific rules and generally outputs results. Therefore, after the data in the stream is processed, the data in the stream needs to be reassembled into a new set. The more common ones are toList, toSet, and toMap, as well as complex toCollection, toConcurrentMap, and so on.

// Obtain the Student name and form a new list collection. List<String> studentNames1 = students.stream().map(Student::getName).collect(Collectors. Set<Integer> ageSet = students.stream().filter(student -> student.getage () >= 15) set <Integer> ageSet = students.stream().filter(student -> student.getage () >= 15) .map(Student::getAge).collect(Collectors.toSet()); // Create a map of Student ids and entities. Map<String, Student> studentMap = students.stream().collect(Student::getId, student -> student)); System.out.println(studentNames1); System.out.println(ageSet); studentMap.forEach((key, value) -> System.out.println(key + ":" + value)); [17, 18, 20, 21, 22, 25] 1:Student{id='1', name=' zhang 3 ', age=18, Sex =male, score=88.0} 2:Student{id='2', name=' 3', age=17, sex=male, score=60.0} 3:Student{id='3', name=' 5 ', age=18, Sex =male, score=100.0} 4:Student{id='4', name=' 6 ', age=20, sex=male, score=10.0} 5:Student{id='5', name=' 7 ', age=14, Sex =female, score=95.0} 6:Student{id='6', name=' 8 ', age=21, sex=male, score=55.0} 7:Student{id='7', name=' 9 ', age=20, Sex =female, score=66.0} 8:Student{id='8', name=' xiao ', age=18, sex=male, score=100.0} 9:Student{id='9', name=' xiao ', Age =22, score=95.0} 10:Student{id='10', name=' xiao zhang ', age=25, sex=male, Score = 90.0} 1234567891011121314151617181920212223242526Copy the code

3.12 Group partitioningBy and groupingBy

PartitioningBy: Elements in the stream are conditionally divided into two maps. GroupingBy: Elements in a stream are grouped into multiple maps based on conditions.

// If the student's score is greater than or equal to 60, Map<Boolean, List<Student>> studentScorePart = students.stream() .collect(Collectors.partitioningBy(student -> student.getScore() >= 60)); // groupingBy gender Map<String, List<Student>> studentSexMap = students.stream().collect(student.groupingby (Student::getSex)); // groupingBy gender Map<String, List<Student>> studentSexMap = students.stream().collect(student.groupingby (Student::getSex)); // groupingBy age Map<Integer, List<Student>> studentAgeMap = students.stream().collect(Collectors. GroupingBy (Student::getAge)); // groupingBy age Map<Integer, List<Student>> studentAgeMap = students.stream().collect(Collectors. // First group by gender, Map<String, Map<Integer, List<Student>>> collect = students.stream().collect( Collectors.groupingBy(Student::getSex, Collectors.groupingBy(Student::getAge))); System.out.println(" if the student score is greater than or equal to 60, divide into 2 groups :"); Studentscorepart. forEach((aBoolean, students7) -> {system.out. println(" the score is greater than or equal to 60? :" + aBoolean); students7.forEach(System.out::println); }); System.out.println(" Grouping by sex :"); Studentsexmap. forEach((sex, students7) -> {system.out.println (" sex? :" + sex); students7.forEach(System.out::println); }); System.out.println(" grouped by age :"); Studentagemap. forEach((age, students7) -> {system.out. println(" age :" + age); students7.forEach(System.out::println); }); System.out.println(" group by sex, then by age :"); Collect. ForEach ((sex, integerListMap) -> {system.out.println (" sex: "+ sex); Integerlistmap. forEach((age, students7) -> {system.out.println (" age :" + age); students7.forEach(System.out::println); }); }); // The output results are divided into two groups according to whether the conditional students' scores are greater than or equal to 60: :false Student{id='4', name=' 6', age=20, sex=male, score=10.0} Student{id='6', name=' 6', age=21, sex=male, Score =55.0} Score greater than or equal to 60? :true Student{id='2', name=' 3 ', age=18, score=88.0} Student{id='2', name=' 4 ', age=17, sex=male, Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score =90.0} Grouping by sex: sex? :female Student{id='7', name=' 8 ', age=14, score=95.0} Student{id='7', name=' 9 ', age=20, sex=female, Score =66.0} Student{id='9', name=' xiaored ', age=22, sex=female, score=95.0} :male Student{id='2', name=' 3 ', age=18, sex= score=88.0} Student{id='2', name=' 4 ', age=17, sex=male, Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score =100.0} Student{id='10', name=' xiaozhang ', age=25, sex=male, score=90.0} Age :18 Student{id='2', name=' 3 ', age=17, sex=male, score=60.0} age :18 Student{id='1', name=' 3 ', age=18, sex=male, Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score = 0, score= 0, score= 0, score= 0, score= 0 Sex =female, score=66.0} age :21 Student{id='6', name=' yaobai ', age=21, sex=male, score=55.0} age :22 Student{id='9', name=' xiaohong ', Age =22, sex=female, score=95.0} age :25 Student{id='10', name=' xiaozhang ', age=25, sex=male, score=90.0} age :14 Student{id='5', Name =' dong Qi ', age=14, sex=female, score=95.0} Gender: Female age :20 Student{id='7', name=' lao9 ', age=20, sex= score=66.0} age :22 Student{id='9', name=' xiaohong ', age=22, Sex =female, score=95.0} age :14 Student{id='5', name=' dong 7 ', age=14, sex=female, score=95.0} Male age :17 Student{id='2', name=' 3 ', age=17, sex=male, score=60.0} Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Score = 0, score= 0, score= 0, score= 0, score= 0, score= 0, score= 0 Sex =male, score=55.0} age :25 Student{id='10', name=' zhang ', age=25, sex=male, Score = 90.0} 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646 566676869707172737475767778798081828384858687888990919293949596979899100101102Copy the code

3.13 combination of joining

Concatenate the elements in stream into a string with the specified concatenate (or directly concatenate if none exists).

String joinName = students.stream().map(Student::getName).collect(Collectors.joining(", ")); System.out.println(joinName); Zhang SAN, Li Si, Wang Wu, Zhao 6, Dong Qi, Yao Ba, Lao Jiu, Xiao Ming, Xiao Hong, Xiao Zhang 12345Copy the code

Original text: blog.csdn.net/chenlixiao0…

Author: Μ r. eta ob &western dy