Stream In Java 8 and Its Features

Posted By : Ankit Shriwastav | 23-Mar-2023

Java java microservices Java Virtual Machine

Loading...

Java provides a new fresh package in Java 8 called java.util.stream. This package i.e. java.util.stream, consists of interfaces, classes and enum to allows functional-style operations on the element. You can use stream by importing java.util.stream package into a project.

Stream provides following features:

  • Stream doesn't store element. It simply transfers elements from a source such as a an array, data structure or an I/O channel, through a channel of computational operations.
  • Stream is functional in nature. Operations performed on a stream doesn't modify it's source. For example, filtering a Stream attained from a collection produces a new Stream without the filtered element, rather than removing elements from the source collection.
  • Stream is lazy and evaluates code only when needed.
  • The elements of a stream are only visited formerly during the entire life of a stream. Like an Iterator, a new stream must be generated to readdress the same elements of the source.
  • You can use stream to convert, print, collect and filter from one data structure to another etc.

Also, Read Stripe Payment Gateway Integration In Java

Stream Creation : There are numerous ways to create a stream instance of different sources. Once created, the instance will not modify its source, thus allowing the creation of multiple instances from a single source.

Empty Stream : We can use the empty() method in case of the creation of an empty stream:
Stream<String> streamEmpty = Stream.empty();

Stream of Collection : We can create a stream of many type of Collection (Set, List, Collection):
Collection<String> collection = Arrays.asList("x", "y", "z");

Stream<String> streamOfCollection = collection.stream();

Stream of Array : An array can be a source of the stream:
Stream<String> streamOfArray = Stream.of("x", "y", "z");

We can create a stream from an existing array or part of an array:

String[] ar = new String[]{"x", "y", "z"};
Stream<String> streamOfArrayFull = Arrays.stream(ar);
Stream<String> streamOfArrayPart = Arrays.stream(ar, x, y);

Stream.builder() : When builder is used, the required type should be additionally mentioned in the right part of the statement, else the build() method will create an instance of the Stream<Object>:

Stream<String> streamBuilder = Stream.<String>builder().add("x").add("y").add("z").build;

Stream.generate() : The generate() method accept a Supplier<T> for element generation. As the resulting stream is infinite(limitless), the developer should mention the required size, or the generate() method will work unless it reaches the memory limit:

Stream<String> streamGenerated = Stream.generate(()->"elements").limit(25);

Also, Read The Important Features In Java 8

Parallel Stream : Before Java 8, parallelization was difficult and complex. The emergence of the Executor Service and the ForkJoin simplified a developer’s life a little bit, but it was still worth flashing back how to create specific executor, how to run it, and others. Java 8 introduced a way of getting parallelism in a different functional style.

The APIs allows us to create parallel streams, which perform operations in a parallel mode. When the source of a stream is an array or a Collection, it could be achieved with the help of the parallelStream() method:

Stream<Product> streamOfCollections = productList.parallelStream();

boolean isParallels = streamOfCollections.isParallels();

boolean bigPrices = streamOfCollections .map(products -> products.getPrices() * 12) .anyMatch(prices -> prices > 400);

If the stream's source is something else than an array or a Collection, the parallel() method should be used:

IntStream intStreamParallels = IntStream.range(1, 200).parallel();

boolean isParallels = intStreamParallels.isParallels();

The stream which is in parallel mode can be converted back to its sequential mode by using the sequential() method:

IntStream intStreamSequentials = intStreamParallels.sequential();

boolean isParallels = intStreamSequentials.isParallels();

The Stream API is simple to understand but a very powerful set of tools for processing the sequence of elements. When it is used properly, it allows the developer to reduce a huge amount of boilerplate code, creates more readable programs, and improves an app’s productivity.