Table of Contents
You can convert list or array to
stream
very easily and perform various operations on top of it.Java 8 Stream provides various methods such as map, Â filter
, reduce
etc.Let’s see more about Java 8 Stream filter method.
Java 8 Stream filter
As name suggests, filter
method is used to filter stream on basis of criterion. You can pass lambda expressions to filter method but it should always return a boolean value
. We have already seen how to pass Predicate object to filter method to filter a collection.
Lets understand more with help of example:Â
Lets say you have Student
class as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package org.arpit.java2blog; public class Student { private int id; private String name; private String gender; private int age; public Student(int id, String name, String gender, int age) { super(); this.id = id; this.name = name; this.gender = gender; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + "]"; } } |
Lets say you have list of student objects in studentList
and you need to filter all male students.
You can do it with the help of Stream with below code:
1 2 3 4 5 6 7 |
// Filter all male students List maleStudents=studentList.stream() .filter(s-> s.getGender().equalsIgnoreCase("M") ) .collect(Collectors.toList()); System.out.println("Male students are :"+maleStudents); |
Here we have used stream‘s filter
method to filter list and then collect the result to another list with Collectors.toList()
.
Java 8 filter,findAny or orElse method
You can use stream’s filter method to filter list and use findAny
and orElse
method based on conditions.
For example:You want to filter Student with name John
, if you do not find it in the list then return null
.
1 2 3 4 5 6 7 8 |
// Filer based on name Student student=studentList.stream() .filter(s-> s.getName().equalsIgnoreCase("John")) .findAny() .orElse(null); System.out.println("Student with Name john :"+student); |
Lets create a main class as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; public class Java8StreamFilterExamples { public static void main(String[] args) { List studentList=createStudentList(); // Filter all male students List maleStudents=studentList.stream() .filter(s>s.getGender().equalsIgnoreCase("M")) .collect(Collectors.toList()); System.out.println("Male students are :"+maleStudents); // Filter based on name Student student=studentList.stream() .filter(s-> s.getName().equalsIgnoreCase("John")) .findAny() .orElse(null); System.out.println("Student with Name john :"+student); } public static List createStudentList() { List studentList=new ArrayList(); Student s1=new Student(1, "Arpit", "M", 19); Student s2=new Student(2, "John", "M", 17); Student s3=new Student(3, "Mary", "F", 14); Student s4=new Student(4, "Martin", "M", 21); Student s5=new Student(5, "Monica", "F", 16); Student s6=new Student(6, "Ally", "F", 20); studentList.add(s1); studentList.add(s2); studentList.add(s3); studentList.add(s4); studentList.add(s5); studentList.add(s6); return studentList; } } |
When you run above program, you will get below output:
Let me know if you need more examples for Stream‘s filter method.