How does the filter() method work in Java 8?

filter() is an intermediate Stream operation that takes a Predicate and returns a new stream containing only the elements for which the predicate evaluates to true.

Key Points

Example

Given a list of numbers, numbers.stream().filter(n -> n % 2 == 0) produces a stream containing only the even numbers.

List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6);

List<Integer> evens = nums.stream()
        .filter(n -> n % 2 == 0)
        .collect(Collectors.toList());

System.out.println(evens); // [2, 4, 6]

Interview Tip

A concise interview answer is

filter() takes a Predicate and returns a new stream that only contains the elements matching that predicate. It's lazy, so it doesn't do any work until a terminal operation runs, and it's commonly used to extract a subset of a collection based on a condition.

Last updated: 16 July 2026

Previous question: What is the use of reduce() in Java 8 Streams?

Next question: What is the significance of Collectors.toList() in Java 8 Streams?

More Java 8 interview questions

All Java 8 questions