Learn to use Stream.filter(Predicate condition) method to traverse all the elements and filter out all elements which do not match a given condition through Predicate argument.
1. Stream filter() method
- This is a intermediate operation.
- Returns a stream consisting of the elements of this stream that match the given predicate.
- The
filter()
argument shall be stateless predicate to apply to each element to determine if it should be included. Predicate
is a functional interface. So, we can also pass lambda expression also.- It returns a new stream so we can use other operations applicable to any stream.
2. Stream.filter() method syntax
The stream()
method syntax is as follows:
Stream<T> filter(Predicate<? super T> condition)
Predicate
is a functional interface and represents the condition to filter out the non-matching elements from the stream.
3. Java Stream filter() examples
3.1. Find all even numbers – Lambda expression
Java example to iterate over stream of integers and print only even numbers.
import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); list.stream() .filter(n -> n % 2 == 0) .forEach(System.out::println); } }
Program output.
2 4 6 8 10
3.2. Find all even numbers – Predicate class
Java example to iterate over stream of integers and print only even numbers. This example uses Predicate class in place of lambda expression, though both are same things.
import java.util.Arrays; import java.util.List; import java.util.function.Predicate; public class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Predicate<Integer> condition = new Predicate<Integer>() { @Override public boolean test(Integer n) { if (n % 2 == 0) { return true; } return false; } }; list.stream().filter(condition).forEach(System.out::println); } }
Program output.
2 4 6 8 10
3.3. Filter even numbers and collect into new list
We can use the collect() method to collect the stream of even numbers and converts it into a list.
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List<Integer> evenNumbers = list.stream() .filter(n -> n % 2 == 0) .collect(Collectors.toList()); System.out.println(evenNumbers); } }
Program output.
[2, 4, 6, 8, 10]
3.4. Filter even numbers and get squares
We can use the map() method to collect the stream of even numbers and then convert each number to it’s square before collecting it to a new list.
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List<Integer> evenNumbers = list.stream() .filter(n -> n % 2 == 0) .map(n -> n * n) .collect(Collectors.toList()); System.out.println(evenNumbers); } }
Program output.
[4, 16, 36, 64, 100]
Drop me your questions related to Java Stream filter() method in Java Stream API.
Happy Learning !!
Leave a Reply