Looking for an Expert Development Team? Take two weeks Trial! Try Now
Stream API was introduced in the 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.).
In this blog, Outsource java development team discuss the intermediate operations/methods present in the Stream API its popular functions and operations.
This method is called the intermediate method because the result or return type of this method is a Stream itself.
List of intermediate methods are:
This method is called the terminal method because the return type of this method 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.
filter():
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 for the 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
In line 11, you will see I am creating a List of Indian city names.
At line 13, I have called the 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.
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 namely 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 the sorted () function on the gameList stream, and finally, we are collecting the result using collect (Collectors.toList()).
The 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 that we have created by calling the parallel () method, we try to peek () and see the result and format it. And then count() it.
limit() method example
The limit() method allows the developer to limit the number of elements to be extracted from a stream.
It is useful in that application where the user wishes to process only the initial elements that occur in the 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 outsourcing 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.