Skip to main content

Stream API in Java 8

Stream API is another powerful feature added in Java 8 which changes the whole programming style. The interfaces and classes related to Stream API are present in the package "java.util.stream" which are used for processing sequences of objects. Before moving ahead it is highly recommended that you should have basic knowledge of Lambda expression, Functional Interfaces, and Method Reference. Please go through these above topic links before moving ahead.

What are Streams in Java 8?

Streams are sequence or series which are obtained from a different type of sources for example Collections, Arrays, group of values or from an input/output source. Stream API is used for processing the collection of objects or groups of values efficiently and allows us to execute multiple operations on it. Streams and collections differ from each other in many ways
  • Collections are based on some data structures that store elements while streams are not a data structure. Streams carry elements from a source through a pipeline of some computational operations.
  • Any operation performed on the collection can modify it while any operation performed on the stream produces a result, but does not modify the source from which the stream obtained.
  • Streams operations are classified into two types of operations (i) intermediate operations(produces a new stream) (ii) terminal operations(produces a final result). Intermediate operations are always lazy in streams. Intermediate operations would not be invoked until the terminal operations are invoked. This type of concept is not available in collections.
  • Streams are not finite in size while collections are finite. Elements in the collection are visible any number of times till they removed from the collection but in case of stream, an element can be visible only once, and to visit the element again new streams must be created again on the same source.

How to create Streams?

Stream instances can be created in so many ways from different sources. You can create multiple stream instances for a single source because once the instance created it will not modify the source.
Stream instances cab be created
  • from any type of collection using stream() and parallelStream() methods present inside the Collection interface in java 8.
  • from an array of any type using method Arrays.stream(T[] array) which is an overloaded static method present inside Arrays class in java 8. For example Arrays.stream(int[] array), Arrays.stream(double[] array) etc.
  • using the static factory methods of Stream interface (i) Stream.of(T... values), (ii) Stream.builder() (iii) Stream.empty() (iv) Stream.of(T t) (v) Stream.iterate(final T seed, final UnaryOperator<T> f) (vi) Stream.generate(Supplier<T> s) (vii) Stream.concat(Stream<? extends T> a, Stream<? extends T> b) (viii) range() and rangeClosed() of IntStream and LongStream.
  • from String objects using a default method chars() which is present inside the interface CharSequence.
  • from path of files using methods present inside class Files. For example lines(Path path) and many more.
  • using BufferedReader.lines(), it produces a stream of lines of a file.
Below is the example of the above methods of stream creation.

What are stream operations and pipelines?

Stream operations are classified into two types (i) intermediate (ii) terminal operations. A stream pipeline is a combination of a stream source, an intermediate operation, and terminal operation. 
Intermediate operations always produce a new stream, for example, filter() operation. It always produces a new stream that contains the objects of the previous stream that matches the condition passed to the filter() method. The filter() method accepts a predicate(a pre-defined functional interface). Intermediate operations are lazy in nature because the execution of intermediate operations doest not invoked until the terminal operation is executed. 
Intermediate operations further classified into two categories (i) stateless and (ii) stateful operations. Stateless operations, for example, filter() and map() process a new object without retaining the state of a previously processed object. Every object is processed separately from the operations of other objects. Now if any information of a previously processed object is needed for processing a new object then these operations are called stateful operations. For example, sorting a stream, the result of a sorting operation on a stream cannot be produced until all the objects are processed by that operation and in this case state of every processed object is needed for processing the next object. Intermediate operations are 
  • Stateful operations: sorted(), distinct(), limit(), skip()
  • Stateless operations: filter(), map(), unordered(), peek()
Terminal operations are also known as the end of the stream on which they are traversed. Terminal operations may or may not produce results. When the execution of the terminal operation is completed then the stream pipeline is said to be consumed. You cannot traverse the same stream again if the terminal operation is executed already. For traversing the same data source again, you need to create the stream again. Terminal operations are eager in nature.
Terminal operations are collect(), reduce(), forEach(), toArray(), min(), max(), count(), anymatch(), allMatch(), noneMatch(), findFirst(), findAny()
Now, let's take an example to understand both intermediate and terminal operations.
Now I am ending up this post and will update it or create a new post for some features of the streams like parallel processing, Ordering, short-circuiting, etc. Also, you can share your queries and feedback in the comment section below. Thank you so much.

Comments

Popular posts from this blog

How to add an existing project to a GitHub Repository

  Add Existing project to a GitHub Repository Step 1: Login into your GitHub account and create a new repository. Step 2: Go to your project folder on your machine and open git bash. After that runs the command “ git init ”. This command initialized the empty git repository on your local machine. Step 3: Create a file “. gitignore ” using the command “ echo > .gitignore ”. Add the file extension or file name inside this file, those you don’t want to commit to your repository. Git will ignore all those files that will be present inside this file. Step 4: Add all the files to the staging area using the command “git add .” This command adds all the files to the staging area and all these files are now ready for the commit. And with the help of the command “git status” , you can check that all the files are inside the staging area. Also, you can see the file extension or the file name mentioned inside the “.gitignore”  are ignored by git. Step 5: Commit the files that are pre...

Lambda Expression

What is Lambda Expression? In java 8, a powerful concept is added named Lambda Expression.  Lambda expression is an anonymous function (a nameless function that does not have a modifier, return type, and even the type of its arguments).  Lambda expression is that you can pass to a method whose argument type is functional interface i.e. an interface that is either annotated by @FunctionalInterface  annotation e.g. interfaces from java.util.function package, or an interface with a single abstract method e.g. Comparable, Comparator, Runnable, Callable, ActionListener, etc.  Lambda expression allows passing customized code to the java method that was not possible before java 8. Although before Java 8 the only option was to wrap your code as an object and pass it using anonymous inner class to the method. Lambda expression defines parameters and code that executed using those parameters. Let's take the simplest example of the lambda expression (String input) -> ...

How to install IntelliJ IDEA on Windows?

System Requirements and steps to install IntelliJ IDEA on Windows System Requirements:  Step 1: Download IntelliJ IDEA. Click here. Step 2: Run the downloaded exe file and click on next. Step 3: Check the checkboxes mentioned in the below image and click on next and then hit the install button. Step 4: Final step is to reboot your system before start using IntelliJ IDEA. Thank you, guys. Please do comment below if there is any query or feedback.