An overview of the

A new abstraction has been added to the Java8 API called a Stream that handles data in a declarative manner.

Stream is a completely different concept from InputStream and OutputStream in the java.io package. Stream in Java8 is an enhancement to collection objects, focusing on very convenient and efficient aggregation operations, or bulk data operations on collection objects.

Like an advanced version of an iterator, a Stream implicitly iterates internally and converts data when the user tells it what to do with its contained elements.

A Stream differs from an iterator in that it can operate in parallel, meaning that the data is divided into segments, each of which is processed in a different thread, and the results are output together.

Let’s start with an example:

 

From this example, you can see the simplicity of the code after introducing the flow operation.

use

The steps to use Stream are as follows:

  1. Create a Stream
  2. To convert a stream to another stream by one or more intermediate operations
  3. Obtain the result by aborting the operation.

1. Create the Stream

Collection class Collection:

  • Stream (): Creates a serial stream
  • ParallelStream (): Creates parallel streams

Array:

Arrays.stream(ARR): A serial stream of Arrays obtained statically

Array.stream (arr, int, int): Returns a serial stream of Arrays, specifying array ranges

Digital categories:

IntStream: an integer stream interface with the following static methods:

  • Empty (): Gets an empty serial stream
  • Generate (IntSupplier): Returns an infinite stream of digits generated by the specified interface
  • of(int,int…) : returns a serial stream of the specified number
  • Of (int): Returns a serial stream containing the specified number
  • Range (start, end): Returns a stream of integers from start to end, closed before open
  • RangeClosed (start, end): Returns a stream of integers from start to end, closed before and closed after

DoubleStream: a stream interface for decimals with the following static methods:

  • of(double,double…) : returns a serial stream of the specified number
  • Of (double): Returns a serial stream containing the specified number
  • Generate (IntSupplier): Returns an infinite stream of digits generated by the specified interface

LongStream: a long-shaping stream interface with the following static methods:

  • Generate (IntSupplier): Returns an infinite stream of digits generated by the specified interface
  • of(long,long…) : returns a serial stream of the specified number
  • Of (long): Returns a serial stream containing the specified number
  • Range (start, end): Returns a stream of integers from start to end, closed before open
  • RangeClosed (start, end): Returns a stream of integers from start to end, closed before and closed after

Random: Random number, there is the following method to generate the stream (not static method)

  • Doubles (): Returns an infinite stream of random numbers between 0 and 1
  • Double (long): Returns a given number of streams of random numbers between 0 and 1
  • Doubles (start, end): Returns an infinite stream of random numbers between start-end
  • Doubles (long, start, end): Returns a given number of random number streams, with values between start-end
  • Longs () is the same as Longs ()

And so on, please try it yourself

2. Flow operation

Stream: interface for a Stream that defines the following methods for streaming intermediate operations:

  • Distinct (): Unduplicate elements in a stream, via equals
  • Filter (function): Filters elements in the stream, removing elements that return false through the method
  • Limit (long): Intercepts the first long element of the stream
  • Skip (long): Discards the first long elements in the stream
  • Map (function): Evaluates each element in a stream through a method
  • MapToDouble (function): Converts each element in the stream to a Double by a method
  • MapToInt (function): Converts each element in the stream to an Int using a method
  • MapToLong (function): Converts each element in the stream to a Long using a method
  • Sorted (): Elements in the stream are sorted according to their natural order
  • Sorted (function): Sorts elements in a stream based on the comparison results of methods

Of course there are other operations

3. Obtain the result

Stream: interface for a Stream that defines the following methods for streaming results:

  • ForEach (function): traverses elements in the stream
  • AllMatch (function): Checks if all elements in the stream are true through functions
  • AnyMatch (function): Checks if there is any original appeal in the stream that is returned true by the function
  • NoneMatch (function): Checks whether all elements in the stream pass through the function
  • Count (): The number of elements in the statistics stream
  • Max (function): Returns the maximum value after the specified function is compared
  • Min (function): returns the minimum value after comparing specified functions
  • ToArray (): Converts a stream to an array

Of course, there are other operations, more operations, please try