Last updated on August 6th, 2024
This feature was introduced in Java 8. Java 8 Stream API is used to process the collection of objects. This is available in the “java.util.stream” package.
What are the various methods in Java 8 Stream API?
It has various methods are:
- filter()
- map()
- collect()
- count()
- sorted()
- sorted(Comparator)
- min(Comparator)
- max(Comparator)
- forEach()
- toArray()
- of() etc..
Example without using Stream API
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(10);
al.add(3);
al.add(5);
al.add(20);
al.add(40);
System.out.println("List of Numbers are:"+al);
ArrayList<Integer> listofOdd = new ArrayList<Integer>();
for (Integer i : al) {
if (i % 2!= 0) {
listofOdd.add(i);
}
}
System.out.println("List of Odd Numbers are:"+listofOdd);
}
}
Output:
List of Numbers are:[10, 3, 5, 20, 40]
List of Odd Numbers are:[3, 5]
- In this above example, we can print odd numbers of a list without using Stream API.
Stream API Methods With Example
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
ArrayList < Integer > listofN = new ArrayList < Integer > ();
listofN.add(10);
listofN.add(0);
listofN.add(5);
listofN.add(20);
listofN.add(30);
System.out.println("List of Numbers are:"+listofN);
List < Integer > l = listofN.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
System.out.println("List of Even Numbers are:"+l);
}
}
Output:
List of Numbers are:[10, 0, 5, 20, 30]
List of Even Numbers are:[10, 0, 20, 30]
- In the above example, we can print an even number of a list using Stream API. The code is concise as compared to without using Stream API.
filter()
This method is used to filter the elements according to our required condition and pass the Predicate as an argument. The predicate is a Predefined Functional Interface. The predicate can take a boolean argument and return a boolean value.
Example:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> listofmks= new ArrayList<Integer>();
listofmks.add(60);
listofmks.add(50);
listofmks.add(70);
listofmks.add(40);
listofmks.add(30);
System.out.println("List of Marks:"+listofmks);
List<Integer> filterList = listofmks.stream().filter(i -> i > 40).collect(Collectors.toList());
System.out.println("List of Marks greater than 40:"+filterList);
}
}
Output:
List of Marks:[60, 50, 70, 40, 30]
List of Marks greater than 40:[60, 50, 70]
map()
This method can take Function as an argument. To perform some operations and return some value then we use the map(Function) method.
Example:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> listofmks = new ArrayList<Integer>();
listofmks.add(60);
listofmks.add(50);
listofmks.add(70);
listofmks.add(40);
listofmks.add(30);
System.out.println("List of marks:"+listofmks);
List<Integer> graceList = listofmks.stream().map(i->i+5).collect(Collectors.toList());
System.out.println("List of marks (marks+5):"+graceList);
}
}
Output:
List of marks:[60, 50, 70, 40, 30]
List of marks (marks+5):[65, 55, 75, 45, 35]
collect()
This method is used to collect the input and convert it into a new List.
List<Integer> graceList = listofmks.stream().map(i->i+5).collect(Collectors.toList());
count()
It returns how many objects are present in the stream.
Long marksCount=listOfMrks.stream().count();
sorted()
This method is used to sort in default natural sorting order the list with the stream.
List<Integer> sortedList = listofmks.stream().sorted().collect(Collectors.toList());
sorted(Comparator)
This method is used to sort in customized sorting order the list with the Stream API.
List<Integer> sortedList = listofmks.stream().sorted((i1,i2)->((i1<i2)?1:(i1>i2)?-1:0)).collect(Collectors.toList());
min(Comparator)
This method returns the minimum element of the stream based on the provided Comparator.
Integer min = listofmks.stream().min(Integer::compare).get();
This method is returning the minimum element in ascending order but in descending order, it returns the first element of the stream.
Integer min = listofmks.stream().min((i1,i2)->((i1<i2)?1:(i1>i2)?-1:0)).get();
max(Comparator)
The method returns the maximum element of the stream based on the provided Comparator.
Integer max = listofmks.stream().max(Integer::compare).get();
This method is returning the maximum element in ascending order but in descending order, it is returning the last element of the stream.
Integer max = listofmks.stream().max((i1,i2)->((i1<i2)?1:(i1>i2)?-1:0)).get();
forEach()
If we can perform some required functionality in every element available in the stream then we can use this method.
Like we can print each element of the list.
Example:
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList < Integer > listofmks = new ArrayList < Integer > ();
listofmks.add(60);
listofmks.add(50);
listofmks.add(70);
listofmks.add(40);
listofmks.add(30);
System.out.println("List of marks:" + listofmks);
System.out.println("Lambda Expression:");
// invoking lambda expression
listofmks.stream().forEach(i -> System.out.println(i));
System.out.println("Method Reference:");
//invoking method reference
listofmks.stream().forEach(System.out::println);
}
}
Output:
List of marks:[60, 50, 70, 40, 30]
Lambda Expression:
60
50
70
40
30
Method Reference:
60
50
70
40
30
toArray()
To convert a stream of objects into an array type.
Example:
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList < Integer > listofmks = new ArrayList < Integer > ();
listofmks.add(60);
listofmks.add(50);
listofmks.add(70);
listofmks.add(40);
listofmks.add(30);
//constructor reference
Integer[] i = listofmks.stream().toArray(Integer[]::new);
System.out.println("List Of marks:");
for (Integer i1: i) {
System.out.println(i1);
}
}
}
Output:
List Of marks:
60
50
70
40
30
of()
This method is used to create a stream of an array type.
Example:
import java.util.ArrayList;
import java.util.stream.Stream;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> listofmks = new ArrayList<Integer>();
listofmks.add(60);
listofmks.add(50);
listofmks.add(70);
listofmks.add(40);
listofmks.add(30);
//constructor reference
Integer[] i=listofmks.stream().toArray(Integer[]::new);
System.out.println("List of marks:");
Stream.of(i).forEach(System.out::println);
}
}
Output:
List of marks:
60
50
70
40
30
Conclusion
This topic is explained What is Stream? How to create a Stream? How does the Stream API concise the code? What are the various methods in Stream?
Its such as you read my mind! You appear to understand so much about this, like you wrote the e-book in it or something. I feel that you just could do with a few percent to force the message house a bit, however other than that, that is great blog. An excellent read. I will certainly be back.