Java 8 Stream API
Java 8 Stream API
Stream API is another new feature of Java 8. All the classes and interfaces of this Stream API is in the java.util.stream package. By using this package, the programmer can perform various aggregate operations on the data returned from collections, arrays, and I/O operations.
Let’s understand it with examples:
1. Use of Stream API map () function to get square of each number present in ArrayList.
import java.util.function.*;
public class Main {
public static void main (String args [])
{
List<Integer> li = new ArrayList<Integer> ();
li.add(10);
li.add(20);
li.add(30);
li.add(40);
List<Integer> ll =li.stream().map((x)->x*x).collect(Collectors.toList());
System.out.println(“List of number are=”+li);
System.out.println(“Square of list of number are=”+ll);
}
}
Explanation of code:
This stream () function converts all list elements into a stream, then map () function perform a square operation on each element of a list, and then at last collect () function again convert all stream elements to list elements.
2. Use of Stream API filer () and map () function to get square of selected number present in ArrayList.
import java.util.function.*;
public class Main {
public static void main (String args [])
{
List<Integer> li = new ArrayList<Integer> ();
li.add(10);
li.add(11);
li.add(30);
li.add(15);
List<Integer> ll =li.stream().
filter((x)->x%2==0).
map((x)->x*x).
collect(Collectors.toList());
System.out.println(“List of number are=” +li);
System.out.println(“Square of list of number are=” +ll);
}
}
Explanation of code:
This stream () function converts all list elements into stream, then filter () function select only even numbers the list as per the given condition for even number, then map () function perform a square operation on filtered even number of the list, and then at last collect () function again convert all stream elements to list elements.
3. Use of Stream API distinct(), limit(), peek() and count() function to get square of selected number present in ArrayList.
import java.util.function.*;
public class Main {
public static void main (String args [])
{
List<Integer> li = new ArrayList<Integer> ();
li.add(10);
li.add(10);
li.add(30);
li.add(30);
li.add(20);
li.add(20);
li.add(50);
li.add(60);
System.out.println(“List of number are=” +li);
li.stream().distinct().limit(3).
peek((x)->{System.out.println(x+” square =”+(x*x));}).
count();
}
}
Explanation of code:
This stream () function converts all list elements into a stream, then distinct () function select unique numbers from the list, then limit () function select first numbers from the list as per parameter assigned in limit() function like 3 in above example. then peek() function perform a task on selected numbers from limit() function and then count function display selected numbers of list line by line.
There are lot more functions in this Stream API, like anyMatch(), allMatch(), noneMatch(), findAny(), findFirst(), toArray(), reduse(), max(), min(), and etc. Details of all these functions are lot of the scope of this app.
Java Stream API Features:
1. Stream does not store the elements; it simply performs the aggregate operations on data.
2. The aggregate operations that perform on the collection array or any other data source do not change the data of the source, they simply return new steam of data.
3. All the stream operations are lazy which means they are not executed until they are needed.
For more information, you can watch this video: