First, what is stream?
Is the data channel that is used to manipulate the sequence of elements generated by the data source (collection, array, and so on). The collection is about the data, and the flow is about computation.
Attention:
Stream does not store elements.
Stream does not change the source object. Instead, they return a new stream that holds the result.
The stream operation is deferred. This means that they will wait until the result is needed to execute.
The three steps of the stream operation:
Create stream: A data source (e.g., collection, array), gets a stream
Intermediate operations: An intermediate chain of operations that processes data from data sources
Terminate operation (terminal action): a terminating operation, performing an intermediate operation chain, and producing the result.
Second, create stream 1, Java8 in the collection interface is extended, provides two ways to get the stream.
Default Stream<e> Stream (): Returns a sequential stream
Default Stream<e> parallelstream (): Returns a parallel stream
// 1. Collection provides two methods of stream () and Parallelstream () new arraylist<>(); stream// get a sequential stream // get a parallel stream
2. Create a stream from an array
The static method of arrays in Java8 stream () can get the array stream.
Static <T> stream<t> Stream (t[] array): Returns a stream
Overloaded form, capable of handling arrays of primitive types:
public static Intstream stream (int[] array)
public static Longstream stream (long[] array)
public static Doublestream stream (double[] array)
// 2. Get an array stream via stream () in Arrays new integer[10]; Stream<Integer> stream1 = Arrays.stream (nums);
3. Create a stream from a value
You can use the static method Stream.of () to create a stream by displaying the value. It can receive any number of parameters.
Public static<t> stream<t> of (T ... values): Returns a stream
stream<integer> stream2 = Stream.of (1,2,3,4,5,6);
4. Create an infinite stream from a function
You can use static methods Stream.iterate () and Stream.generate () to create an infinite stream.
Iterations: Public static<t> stream<t> Iterate (final T seed, final unaryoperator<t> F)
Build: Public static<t> stream<t> Generate (supplier<t> s)
// Iteration stream<integer> stream3 = stream.iterate (0, (x) x + 2). Limit (n); Stream3.foreach (System.out::p rintln);
// Build stream<double> stream4 = stream.generate (math::random). Limit (2); Stream4.foreach (System.out::p rintln);
Third, middle operation of Stream
Multiple intermediate operations can be connected to form a pipeline, unless the pipeline triggers an abort operation, otherwise the intermediate operation will not perform any processing! And all-in-one processing at the end of the operation is called "lazy evaluation".
1. Screening and slicing
①filter
// internal iterations: iterative operation Stream API internal completion @Test public void< /span> Test2 () { // All intermediate operations do not do any processing stream<employee> Stream = Emps.stream (). Filter ((e) -> {System.out.println ( "Test intermediate action" ); return e.getage () <= 35; }); // All intermediate operations are performed all at once as a terminating operation, called "Lazy evaluation" Stream.foreach (System.out::p rintln); }
// External iterations @Test publicvoid test3 () { Iterator<Employee> it = Emps.iterator (); while (It.hasnext ()) { System.out.println (It.next ()); } }
②limit
@Test publicvoid test4 () { emps.stream () { SYSTEM.OUT.PRINTLN (// && | | return e.getsalary () >=; }). Limit (3) . ForEach (system.out::p rintln); }
③skip
@Test publicvoid test5 () { emps.parallelstream () E.getsalary () >= () . Skip (2) . ForEach (system.out::p rintln); }
④distinct
@Test publicvoid Test6 () { emps.stream () . DISTINCT () . ForEach (system.out::p rintln); }
2. Mapping ① Mappings
@Test Public voidtest1 () {Stream<String> str =Emps.stream (). Map ((e)-e.getname ()); System.out.println ("-------------------------------------------"); List<String> strlist = arrays.aslist ("AAA", "BBB", "CCC", "ddd", "Eee"); Stream<String> stream =Strlist.stream (). Map (String::touppercase); Stream.foreach (System.out::p rintln); Stream<Stream<Character>> stream2 =Strlist.stream (). Map (Teststreamapi1::filtercharacter); Stream2.foreach (SM)-{Sm.foreach (system.out::p rintln); }); System.out.println ("---------------------------------------------"); Stream<Character> STREAM3 =Strlist.stream (). FLATMAP (Teststreamapi1::filtercharacter); Stream3.foreach (System.out::p rintln); }
Public Static Stream<character> filtercharacter (String str) { Listnew arraylist<>(); for (Character Ch:str.toCharArray ()) { list.add (ch); } return List.stream (); }
② sort
@Test Public voidtest2 () {emps.stream (). Map (Employee::getname). Sorted (). ForEach (System.out: :p rintln); System.out.println ("------------------------------------"); Emps.stream (). Sorted ((x, y)- { if(X.getage () = =Y.getage ()) { returnx.getname (). CompareTo (Y.getname ()); }Else{ returnInteger.compare (X.getage (), Y.getage ()); }}). ForEach (System.out::p rintln); }
③stream Terminate operation
The terminate operation produces the result from the pipeline of the stream. The result can be any value that is not a stream, for example: List, Integer, or even void.
Find and Match:
Match
@Test Public voidtest1 () {BooleanBL =Emps.stream (). Allmatch ((E)-e.getstatus (). Equals (Status.busy)); System.out.println (BL); BooleanBL1 =Emps.stream (). AnyMatch ((E)-e.getstatus (). Equals (Status.busy)); System.out.println (BL1); BooleanBL2 =Emps.stream (). Nonematch ((E)-e.getstatus (). Equals (Status.busy)); System.out.println (BL2); }
Find
@Test publicvoid test2 () { Optional<Employee> op = Emps.stream () double.compare (E1.getsalary (), E2.getsalary ()) . FindFirst (); System.out.println (Op.get ()); System.out.println ("--------------------------------"); Optional<Employee> op2 = emps.parallelstream () , e.getstatus (). Equals ( Status.free)) . Findany (); System.out.println (Op2.get ()); }
Count, Max, min
@Test publicvoid test3 () { long count = emps.stream () e.getstatus (). Equals (Status.free)) . Count (); System.out.println (count); Optional<Double> op = emps.stream () . Map (employee::getsalary) . Max (double::compare); System.out.println (Op.get ()); Optional<Employee> op2 = emps.stream () double.compare (E1.getsalary (), E2.getsalary ())); System.out.println (Op2.get ()); }
Cannot be used again after a termination operation
// Note: After a stream has been terminated, it cannot be used again @Test publicvoid test4 () { stream<Employee> stream = Emps.stream () , e.getstatus (). Equals (Status.free)); long count = stream.count (); Stream.map (employee::getsalary) . Max (Double::compare); }
Second, Streamapi