Open links in new tab
  1. The filter() method in Java is part of the Stream API introduced in Java 8. It is used to filter elements of a stream based on a given predicate. This method creates a new stream containing only the elements that satisfy the predicate condition.

    Example: Filter Numbers Divisible by 5

    import java.util.Arrays;
    import java.util.List;

    public class Main {
    public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(3, 4, 6, 12, 20);
    numbers.stream()
    .filter(num -> num % 5 == 0) // Filter numbers divisible by 5
    .forEach(System.out::println); // Output: 20
    }
    }
    Copied!

    How It Works

    The filter() method is an intermediate operation, meaning it does not execute immediately but waits until a terminal operation (like forEach or collect) is invoked. It uses a Predicate functional interface to test each element.

    Example: Filter Strings Ending with 's'

    Feedback
  2. Java Stream Filter with Lambda Expression - Baeldung

    In this quick tutorial, we’ll explore the use of the Stream.filter() method when we work with Streams in Java. We’ll look at how to use it, and how to handle special cases with checked exceptions.
    See more on baeldung.com
    • Published: Dec 12, 2018
    • Stream (Java Platform SE 8 ) - Oracle Help Center

        • filter. Stream<T> filter(Predicate<? super T> predicate) Returns a stream consisting of the …
        • map. <R> Stream<R> map(Function<? super T,? extends R> mapper) Returns a stream consisting …
        • mapToInt. IntStream mapToInt(ToIntFunction<? super T> mapper) Returns an IntStream consisting …
        • mapToLong. LongStream mapToLong(ToLongFunction<? super T> mapper) Returns a LongStream …
        • mapToDouble. DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper) Returns a …
    • How to Filter a Collection Using Streams in Java?

      Jun 9, 2025 · By applying the concepts and techniques discussed in this article, you can become proficient in using Java streams to filter collections and improve …

    • Java Stream filter - filtering Java streams - ZetCode

      May 25, 2025 · Java Stream filter tutorial provides a detailed guide on how to use the filter method to efficiently process and filter elements in Java streams. Learn about predicate functions, lambda …

    • Java Stream Filter Example: A Comprehensive Guide

      Jan 16, 2026 · This blog post will explore the fundamental concepts of the Java Stream `filter` method, its usage, common practices, and best-practices to help you gain an in-depth understanding and use …

    • Java Stream filter () Example

      This tutorial explains how to use the filter () method in the Java Stream API. The filter () method is an intermediate operation that tests each element of a stream to see if it matches a given predicate.

    • Java’s Stream.filter () Method Explained - Medium

      Jul 28, 2024 · Learn how to use Java's Stream.filter () method to process collections efficiently, with practical examples on filtering user data and …

    • Java 8 Stream Tutorial - GeeksforGeeks

      3 days ago · Java 8 introduced the Stream API, which allows developers to process collections of data in a functional and declarative way. Streams make it easier to …

    • The Java Stream API Tutorial - Baeldung

      Oct 5, 2023 · Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping …