JDK1.8 Stream

Java 8 API adds a new abstraction called stream Stream, allows you to process the data in a declarative way.

Stream uses a similar intuitive way to use SQL statements from the database query data to provide a higher level of abstraction for Java and set operations expression.

This style element to be viewed as a collection of process flow stream in a pipeline transmission, and can be processed on the node conduit, such as filtering, sorting, polymerization.

Results elementary stream in the pipeline intermediate operating treated (intermediate operation), and finally by a final operation (terminal operation) obtained from a previous process

First, create a stream

  1, Collection interface has a default stream method, this is, we usually use list.stream (), as long as the interface implementation class can go in this way to create a stream

Lists.newArrayList().stream();

  2, Stream of static methods

// 1, created by the build method 

Stream.Builder Builder = Stream.builder (); builder.accept ( 1 ); builder.add ( 2 ); Stream build = builder.build ();
// 2, created by the method Stream.of 
// of overloaded has two methods, one is to accept an object, creating a variable parameter accepts stream Stream S1 = Stream.of (. 1 ); Stream S2 = Stream. of (1,2,3,4,5);
// 3, to create a flow through Stream.generate static method. This is important to note that this method is to create an infinite flow, limit call the method if not call this function to limit the length of the stream, it will continue to create unlimited 

// behavioral parameters passed here must have a return value, that is, behavior the result is an object created.
Generate Stream = Stream.generate (the Math :: Random) .limit (10 ); Stream G2 . Stream.generate = (() -> Math.random ()) limit (10 );
// 4, to create a flow through Stream.iterate iteration, supra flow is unlimited, this difference is that the function has two parameters, the first parameter is the base value, the second parameter is based on calculating the first parameter function. Generating a sequence of natural numbers as 

// the iterate herein, will be the second iteration of the operation parameters,

 Stream limit = Stream.iterate(0, n -> n +1).limit(10);
 // 5, Stream.empty () creates an empty flow
// . 6, Stream.concat (arg1, arg2) the two streams into one stream

 

 

 

Guess you like

Origin www.cnblogs.com/Java-luan/p/11861935.html