Java 8-Java CoreNo Comments

Stream is a new abstract layer introduced in Java 8. Using stream, you can process data in a declarative way similar to SQL statements. For example, consider the following SQL statement −

The above SQL expression automatically returns the maximum salaried employee’s details, without doing any computation on the developer’s end. Using collections framework in Java, a developer has to use loops and make repeated checks. Another concern is efficiency; as multi-core processors are available at ease, a Java developer has to write parallel code processing that can be pretty error-prone.

To resolve such issues, Java 8 introduced the concept of stream that lets the developer to process data declaratively and leverage multicore architecture without the need to write any specific code for it.

What is Stream?

Stream represents a sequence of objects from a source, which supports aggregate operations. Following are the characteristics of a Stream −

  1. Sequence of elements − A stream provides a set of elements of specific type in a sequential manner. A stream gets/computes elements on demand. It never stores the elements.
  2. Source − Stream takes Collections, Arrays, or I/O resources as input source.
  3. Aggregate operations − Stream supports aggregate operations like filter, map, limit, reduce, find, match, and so on.
  4. Pipelining − Most of the stream operations return stream itself so that their result can be pipelined. These operations are called intermediate operations and their function is to take input, process them, and return output to the target. collect() method is a terminal operation which is normally present at the end of the pipelining operation to mark the end of the stream.
  5. Automatic iterations − Stream operations do the iterations internally over the source elements provided, in contrast to Collections where explicit iteration is required.

Generating Streams

With Java 8, Collection interface has two methods to generate a Stream −

  • stream() − Returns a sequential stream considering collection as its source.
  • parallelStream() − Returns a parallel Stream considering collection as its source.

forEach

Stream has provided a new method ‘forEach’ to iterate each element of the stream. The following code segment shows how to print 10 random numbers using forEach.

map

The ‘map’ method is used to map each element to its corresponding result. The following code segment prints unique squares of numbers using map.

filter

The ‘filter’ method is used to eliminate elements based on a criteria. The following code segment prints a count of empty strings using filter.

limit

The ‘limit’ method is used to reduce the size of the stream. The following code segment shows how to print 10 random numbers using limit.

sorted

The ‘sorted’ method is used to sort the stream. The following code segment shows how to print 10 random numbers in a sorted order.

Parallel Processing

parallelStream is the alternative of stream for parallel processing. Take a look at the following code segment that prints a count of empty strings using parallelStream.

It is very easy to switch between sequential and parallel streams.

Collectors

Collectors are used to combine the result of processing on the elements of a stream. Collectors can be used to return a list or a string.

Statistics

With Java 8, statistics collectors are introduced to calculate all statistics when stream processing is being done.

Stream Example

Create the following Java program using any editor of your choice in, say, C:\> JAVA.

Java8Tester.java

Verify the Result

Compile the class using javac compiler as follows −

Now run the Java8Testeras follows −

It should produce the following result −


Be the first to post a comment.

Add a comment