Stream API was introduced in JDK 1.8 release, for the sole purpose to ease the way to deal with collections of objects or we can say that stream of objects/elements stored in a Collection API implementation classes (LIST, MAP, etc.).
This blog we will discuss the intermediate operations/methods present in the Stream API its popular functions and operations.
This method is called intermediate methods because the end result or return type of this method is a Stream itself.
List of intermediate methods are:
This methods are called as terminal methods because the return type of this methods will be a non-stream element, such as primitive value, a collection or no value at all.
One thing to note this method should be or can be called only after intermediate methods in precedence.
List of Terminal methods are:
Let’s understand each intermediate and terminal method in detail.
As the name suggests this intermediate method will filter the Stream elements based on the predicate passed. For example, you want to get only even numbers from the list of numbers you can easily do it using filter () and passing a predicate-argument to filter.
Predicate: It takes predicate reference as an argument. A predicate is a functional interface so we can also pass lambda expression here.
1. Simple filter () method example and we have used forEach() end operations here.
So in the image
At line 11 you will see I am creating a List of Indian city names.
At line 13 I have called stream () method on the names list reference variable, stream () will convert the names list to stream, after stream () if called filter () method and inside the filter method I have passed a predicate name with lambda operator and after that trying to compare the values of predicate reference i.e. name with one of the List value.
Basically I am trying to print all the city names which are not equal to Mumbai and present in the list.
End operation I am doing with the help of forEach() loop which is again accepting a predicate as an argument with Lambda operator
And then trying to print the nameEle predicate which will hold all the city names which are not equal to Mumbai.
2. Filter() method example with findAny() and orElse() termination operations/methods
In the above image again I have created a vehicleList
Again here I am converting vehicleList to stream and then calling filter() method in which trying to compare whether a value in the predicate x is equal to Honda or not.
In Line 17 if “Honda”.equals(x.getName()) returns true then findAny() termination method will be triggered and hence in Line 18 Honda will get printed in the console.
In Line 19 I am checking If “Yamaha”.equals(x.getName()) which returns false, hence orElse() method is triggered and thus null is printed in the console.
findAny() and orElse() method both work like if and else loop.
3. filter() method with map() method code example
So here in the above images what I have done here is created an ExampleVehiclePojo with variables as name, engineCC and manufacturer with setter and getter.
After that in FilterMethodWithMapExample class I have created a list vehicleList of ExampleVehiclePojo type and stored three ExampleVehiclePojo objects with different values.
At line number 16 in the above image, I am converting the vehicleList to stream and then calling filter () method on the stream. In filter method, I am checking whether the “Honda”.equals(x.getName()) and “1000”.equals(x.getEngineCC()) if both matches with the predicate i.e. x values coming from the stream then I am calling map() method, in map() method I am calling ExampleVehiclePojo :: getName which will get the Name from the ExampleVehiclePojo only if “Honda”.equals(x.getName()) and “1000”.equals(x.getEngineCC()) returns true and findAny() method is triggered, otherwise orElse() method will be triggered and which will print null.
Couple of things to note here
4. FlatMap() method example
But the stream operations like filter, sum, distinct, etc. and collectors do not support it,
Here we are converting int to IntStream using flatMap()
And also Set
Sorted() method code
Here we are creating a gameList and then calling sorted () function on the gameList stream and finally we are collecting the result using collect (Collectors.toList()).
Output will be list is sorted in lexicographic order
peek() method example
Here we are trying to find out the number is prime or not. And in the parallel stream what we have created calling parallel () method we trying to peek () and see the result and format it. And then count() it.
limit() method example
limit() method allows the developer to limit the number of elements to be extracted from a stream.
It is useful in those application where the user wishes to process only the initial elements that occur in stream.
Here we have seen that we can use stream API operations on a collection of elements or Arrays.
And we can get our desired result as we want.
Specific points to note
So finally stream API is the API that is introduced in java 1.8 to ease the outsource java development effort and get the desired result while dealing with a collection of elements and get the desired result with less effort and time.