Java 25 (LTS) reference · Stream API

Java Stream filter() Method

Keep only the elements that match a condition. Intermediate and lazy: nothing runs until a terminal operation.

Java 25 (LTS) Since Java 8

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

stream.filter(predicate)

Parameters

ParameterDescription
predicate
Predicate<T>
A test that returns true to keep an element.

Return value

A new Stream<T>.

Examples

Keep matching elements
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> nums = List.of(5, 12, 8, 130, 44);
        System.out.println(nums.stream().filter(n -> n > 10).toList());
    }
}

Output

[12, 130, 44]

Run this on your computer — see “Get Started” for setup.

Filter objects, then map
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        record User(String name, boolean active) {}
        List<User> users = List.of(new User("Ana", true), new User("Ben", false));
        System.out.println(users.stream().filter(User::active).map(User::name).toList());
    }
}

Output

[Ana]

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
Java Java 8Added

Was this page helpful?