Make writing a habit together! This is the sixth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.
Moving on from the previous article on Java8’s new feature 2, let’s move on to some of the other new features.
7. Powerful Stream API
Stream API description:
There are two most important changes in Java8. The first is a Lambda expression; The other is the Stream API.
The Stream API (java.util.stream) brings a true functional programming style to Java. This is by far the best addition to the Java class library, because the Stream API provides a great deal of productivity for Java programmers, allowing them to write efficient, clean, and concise code.
Stream is the key abstraction for dealing with collections in Java8. It can specify what you want to do with collections, and can perform very complex operations like finding, filtering, and mapping data.
Manipulating collection data using the Stream API is similar to database queries executed using SQL. You can also use the Stream API to perform operations in parallel. In short, the Stream API provides an efficient and easy-to-use way to process data.
Why use the Stream API?
In practice, most of the data sources in the project come from Mysql, Oracle, etc. But now there are more data sources, such as MongDB, Radis, etc., and these NoSQL data need to be processed at the Java level.
What is a Stream?
A Stream is a data channel used to manipulate sequences of elements generated by data sources (collections, arrays, and so on). “Collections are about data, streams are about computation!”
Note: ①Stream does not store elements by itself. ②Stream does not change the source object. Instead, they return a new Stream holding the result. ③ The Stream operation is delayed. This means they wait until they need results.
Stream operates in three steps:
- 1 – create the Stream
A data source (e.g., collection, array) that retrieves a stream
- 2- Intermediate operation
An intermediate chain of operations that processes data from the data source
- 3- Termination operation (terminal operation)
Once the termination operation is performed, the intermediate operation chain is executed and the result is produced. After that, it won’t be used again
1 – create the Stream
The first way to create a Stream is through a collection
The Collection interface in Java8 has been extended to provide two methods for retrieving streams:
- Default Stream Stream () : Returns a sequential Stream
- Default Stream parallelStream() : returns a parallelStream
The second way to create a Stream is through an array
The Java8 static stream() method of Arrays fetches array streams:
- Static Stream Stream (T[] array): Returns a Stream
An overloaded form that can handle arrays corresponding to primitive types:
- public static IntStream stream(int[] array)
- public static LongStream stream(long[] array)
- public static DoubleStream stream(double[] array)
Create a Stream through the of() of a Stream
You can call the Stream class static method of() to create a Stream by displaying values. It can accept any number of parameters.
- public static Stream of(T… Values) : Returns a stream
Create Stream 4: Create an infinite Stream
You can use the static methods stream.iterate () and stream.generate () to create an infinite Stream.
- The iteration
public static Stream iterate(final T seed, final UnaryOperator f)
- generate
public static Stream generate(Supplier s)
2- Intermediate operation
Multiple intermediate operations can be joined together to form a pipeline that does not perform any processing unless termination is triggered on the pipeline! When the operation terminates, it is processed all at once, which is called lazy evaluation.
1- Screening and sectioning
2 – reflected beam
3 – sort
3- Termination operation (terminal operation)
Terminal operations generate results from the pipeline of streams. The result can be any value that is not a stream, such as List, Integer, or even void.
After a stream terminates, it cannot be used again.
1- Match and find
2 – reduction
Note: The connection between Map and Reduce is commonly known as the map-reduce mode, which is famous for Google’s use of it for web searches.
3 – to collect
The implementation of methods in the Collector interface determines how the collected operations (such as collecting lists, sets, maps) are performed.
In addition, the Collectors class provides many static methods to easily create common collector instances. The following table describes the methods and instances.