Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of Stream in Java8

Shulou Source: shulou.com Published: 2022-05-31 19:00:19 10月03日 Update

This article mainly introduces "what is the use of Stream in Java8". In daily operation, I believe that many people have doubts about the use of Stream in Java8. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "what is the use of Stream in Java8?" Next, please follow the editor to study!

1. Why experienced veterans are more likely to use Stream

Performance advantage, (large amount of data) faster than iterator

Serial and parallel processing are supported, and parallel processing can make full use of CPU resources.

Stream is a stream of computed data that does not store data itself.

Support for functional programming

Elegant code, making the code more efficient, clean and concise

2. How to use Stream

Three steps:

Create Stream

Intermediate operation

Terminate the operation

3. The creation of Stream

The creation of Stream depends on the data source, usually container or array Stream flow creation is roughly divided into 4, the most commonly used is to create through the collection

Import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.stream.IntStream;import java.util.stream.Stream;public class CreateStreamDemo {public static void main (String [] args) {/ / 1 the most frequently used form of creating Stream through collections is List strList = new ArrayList (); strList.add ("a"); strList.add ("b") StrList.add ("c"); / / create a serial operation flow Stream stream = strList.stream (); / / create a parallel operation flow Stream parallelStream = strList.parallelStream (); / / 2 create Stream int [] arr = new int [] {1J 2 arr} through an array; IntStream intStream = Arrays.stream (arr) / 3 through Stream.of Stream integerStream = Stream.of (1jue 2jue 3); Stream stringStream = Stream.of ("a", "b", "c"); / / 4 infinite stream / / take one Stream.iterate (0, t-> t + 5) every five numbers. ForEach (System.out::println) / / iterate Stream.generate (Math::random) .forEach (System.out::println); / / generate}} 4. Stream intermediate operation

Stream intermediate operations, we are most commonly used is filtering, de-duplicating, sorting this chapter contains the most commonly used in our development of object de-weight, and more according to the combination of attributes in the object sorting

Import com.zhj.java8.bean.Student;import java.util.ArrayList;import java.util.Comparator;import java.util.List;import java.util.TreeSet;import java.util.stream.Stream;import static java.util.stream.Collectors.collectingAndThen;import static java.util.stream.Collectors.toCollection;public class MiddleStreamDemo {public static void main (String [] args) {List students = new ArrayList (); students.add (new Student (1, "Xiaohua", 23Jing 1)) Students.add (new Student (1, "Xiaohua", 23); students.add (new Student (2, "Xiaomi", 20)); students.add (new Student (3, "Xiaoguo", 30)); students.add (new Student (4, "Xiaowei", 18)); / / filter students.stream (). Filter (stu-> stu.getAge () > 20) .forEach (System.out::println) / / de-duplicate / / A pair of objects is deduplicated according to reference, and repetition is not removed unless overridden equals and hashCode methods System.out.println ("- deduplicated -"); System.out.println ("deduplicated 1 -") Students.stream (). Distinct (). ForEach (System.out::println); / / A pair of objects in the collection do not override some properties, do not override the equals and hashCode methods, and can only use other data structures to assist in deduplicating / / single attributes can be stu-> stu.getId () / / multiple attributes can be stu-> stu.getId () + " "+ stu.getName () System.out.println (" de-weight 2-"); ArrayList distinctList = students.stream () .collect (collectingAndThen (toCollection ()-> new TreeSet (Comparator.comparing (stu-> stu.getId () +"; "+ stu.getName (), ArrayList::new)); distinctList.stream () .forEach (System.out::println) / / sorting supports defining sorting methods / / sorted defaults to natural sorting, where elements must implement the Comparable interface System.out.println ("- sort -"); System.out.println ("sort 1 -") Students.stream (). Sorted (). ForEach (System.out::println); / / sorted (Comparator)

Tags: Element sort age student data method maximum function attribute multiple object commonly used condition Xiaohua learning iteration that is method grouping processing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL Microsoft Shulou Technology macOS