Quick Go Through of JAVA 8 Streams

Java 8 implements many features of functional programming such as Optional, Completable Futures, Streams, Lambdas and many more. All these Features have created a language which is more readable and more concise. As Java 8 is giving more importance to Concurrency. So, Java 8 has developed Streams API which leads us to peep in the window of parallelism.

The Stream is a new abstract layer introduced in Java 8. Streams are the first library to take advantage of the power of lambda expressions in Java, but there’s nothing magical about it as it is tightly integrated into the core JDK libraries. Streams are the part of java.util.stream package.

What is Streams?

In layman terms, a stream is a sequence of data elements made available over time. And stream technically represents a sequence of objects from a source. Java 8 Streams concept is based on converting Collections to a Stream (i.e processing the elements in parallel and then gathering the resulting elements into a Collection).

Streams VS collections:

  • A collection is a data structure. It basically concerns with an organization of data in memory. Collection also persists over a period of time. Whereas Streams is not a data structure, Streams focus is on Computation rather than data. Data come from elsewhere (i.e collection, array or any other data source).
  • Streams can be infinite whereas Collection can be finite.
  • Operations on collections are eager and mutative whereas in case of streams only the terminal operations are eager and intermediate operations are Lazy.
  • Streams provide a wide scope for performing the operations providing the different execution strategies i.e laziness, short-circuiting, and operation fusion. Whereas collection doesn’t support.
  • An operation on stream produces a result but does not modify its source. Whereas collection does modify its source.
  • In Streams, elements are only visited once during the life of the stream. Whereas in Collection, we can revisit the element.
  • Streams are more optimized as we can use multiple operations in a single go and all the three execution strategies can be applied together. Whereas in Collection we have to write much lengthy code to do so.

Stream Operations :

Basically, there are two types of operationsJava 8 streams (i.e 1. Intermediate operation and 2. Terminal operation). Any operation involving the use of Java 8 Streams API has to have three important components to make it work. These are – a source, one or more intermediate operations and a terminal operation. These three types of components are pipelined in a sequence to make a stream work. Here source is the source of data from which a stream is generated.

1. Intermediate operations

Intermediate operations are invoked on a Stream instance and after they finish their processing they give a Stream instance as output. Also, some streams take one stream as a input and returns a different streams as output depending upon the type of intermediate operations. Following are the list of intermediate operations:

Image1

Further Intermediate operation are divided into two types i.e Stateful operations and stateless operations.

  • Stateful operations : Those operations which maintain information from a previous invocation internally, such that can be used again in a future invocation of the method. Such as Stream.distinct().
  • Stateless operations: These operations does not maintain information from the previous invocation. Such as findAny().

2. Terminal operations :

Terminal operations are responsible for giving the final output for a Stream in operation, and in the process, they terminate a Stream. Terminal operation can also return any value, or even no value(i.e void). Following are the list of intermediate operations:

Image2

Conclusion :

In this blog, we understood what is Streams, Streams operations, the difference between Collections and Streams, and how actually Stream works using its basic three component.

References :

https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
https://www.ibm.com/developerworks/library/
Pasted image at 2017_11_27 04_17 PM

Written by 

Ayush Aggarwal is a Software Consultant having experience of more than 11 months. He has knowledge of various programming languages such as JAVA, C and C++, and also has good knowledge of database technologies like MySQL. He has worked upon Object Oriented Paradigm and a Java and Android enthusiast. His hobbies include playing football, basketball etc.

Leave a Reply

%d bloggers like this: