Table of contents
Overview
Syntax : Stream filter(Predicate predicate)
Usage : Filtering elements based on some condition .
Example : From a list of numbers if you want to filter out even or odd numbers then you can use filter method .
filter method Operates On : Stream . Ex : stream.filter(predicate)
Parameters : Predicate interface [ It Represents a function which returns a boolean value ] .
Returns : It returns a new stream with filtered values .
package filter; import java.util.List; import static java.util.stream.Collectors.*; public class Filtering { public static void main(String[] args) { List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List<Integer> evenNumbers = list.stream() .filter(number -> number % 2 == 0) .collect(toList()); System.out.println(evenNumbers); } } O/P: [2, 4, 6, 8, 10]
We can separate out the predicate logic and pass it to the filter method.
package filter; import java.util.List; import java.util.function.Predicate; import static java.util.stream.Collectors.*; public class Filtering { public static void main(String[] args) { List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Predicate<Integer> filterEvenNumbers = (number) -> number % 2 == 0; List<Integer> evenNumbers = list.stream() .filter(filterEvenNumbers) .collect(toList()); System.out.println(evenNumbers); } } O/P : [2, 4, 6, 8, 10]
Java stream filter multiple conditions
It is possible to filter the elements with multiple conditions . Just make sure a boolean is a return from the predicate interface method .
Example : Filter out the integers which are divible by 2 and 3 .
package filter; import java.util.List; import java.util.function.Predicate; import static java.util.stream.Collectors.*; public class Filtering { public static void main(String[] args) { List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Predicate<Integer> filterNumbers = (number) -> number % 2 == 0 && number % 3 == 0; List<Integer> evenNumbers = list.stream() .filter(filterNumbers) .collect(toList()); System.out.println(evenNumbers); } } O/P: [6]
Multiple filters in Java 8
It is possible to use multiple filters in Java 8 .
Example : Filter the integers which are divisible by 2 then ,
Filter the integers which are divisible by 3
package filter; import java.util.List; import java.util.function.Predicate; import static java.util.stream.Collectors.*; public class Filtering { public static void main(String[] args) { List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Predicate<Integer> filterNumbersDivisibleBy2 = (number) -> number % 2 == 0; Predicate<Integer> filterNumbersDivisibleBy3 = (number) -> number % 3 == 0; List<Integer> evenNumbers = list.stream() .filter(filterNumbersDivisibleBy2) .filter(filterNumbersDivisibleBy3) .collect(toList()); System.out.println(evenNumbers); } } O/P: [6]
java 8 filter list of objects by property
We have a list of Persons . Filter out persons whose age is greater than 30 .
Class : Person
Properties : Name , Age
Condition : Age greater than 30
package filter; public class Person { private String name; private int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
package filter; import java.util.List; import static java.util.stream.Collectors.*; public class Filtering { public static List<Person> createPeople() { return List.of( new Person("Suraj", 30), new Person("Iqbal", 20), new Person("Amit", 31), new Person("Suchit", 73), new Person("Amar", 11), new Person("Suraj", 22), new Person("Bhaskar", 35) ); } public static void main(String[] args) { List<Person> personsOlderThan30 = createPeople().stream() .filter(person -> person.getAge() > 30).collect(toList()); System.out.println(personsOlderThan30); } } O/P : [Person [name=Amit, age=31], Person [name=Suchit, age=73], Person [name=Bhaskar, age=35]]
java 8 filter list based on another list
It is possible to filter a list based on another list in Java 8 .
Example : List 1 : Contains Person ( Name and Age )
List 2 : Contains only Age
Criteria : Filter Persons from List 1 if there age is available in List 2
package filter; import java.util.List; import static java.util.stream.Collectors.*; public class Filtering { public static List<Person> createPeople() { return List.of( new Person("Suraj", 30), new Person("Iqbal", 20), new Person("Amit", 31), new Person("Suchit", 73), new Person("Amar", 11), new Person("Suraj", 22), new Person("Bhaskar", 35) ); } public static void main(String[] args) { List<Integer> list = List.of(5, 10, 15, 20, 25, 30, 35, 40, 45, 50); List<Person> filteredList = createPeople().stream() .filter(person -> list.contains(person.getAge())).collect(toList()); System.out.println(filteredList); } } O/P : [Person [name=Suraj, age=30], Person [name=Iqbal, age=20], Person [name=Bhaskar, age=35]]
Does Java stream filter preserve order ?
It depends on what collection you are passing to the stream . Say If you are passing a list to a stream like the above examples then the order will be preserved but if you are passing a set to the stream then there is no guarantee if the order will be preserved .
package filter; import java.util.List; import java.util.Set; import static java.util.stream.Collectors.*; public class Filtering { public static Set<Person> createPeople() { return Set.of( new Person("Suraj", 30), new Person("Iqbal", 20), new Person("Amit", 31), new Person("Suchit", 73), new Person("Amar", 11), new Person("Suraj", 22), new Person("Bhaskar", 35) ); } public static void main(String[] args) { List<Integer> list = List.of(5, 10, 15, 20, 25, 30, 35, 40, 45, 50); List<Person> filteredList = createPeople().stream() .filter(person -> list.contains(person.getAge())).collect(toList()); System.out.println(filteredList); } } O/P : [Person [name=Bhaskar, age=35], Person [name=Suraj, age=30], Person [name=Iqbal, age=20]]
Hope you liked this article on the Java stream filter method . If you want to add anything extra to this article please comment below . You can also write to me if you want to learn any specific topic on Java 8 .