Open In App

Stream filter() in Java with examples

Last Updated : 09 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Stream filter(Predicate predicate) returns a stream consisting of the elements of this stream that match the given predicate. This is an intermediate operation. These operations are always lazy i.e, executing an intermediate operation such as filter() does not actually perform any filtering, but instead creates a new stream that, when traversed, contains the elements of the initial stream that match the given predicate. 

Syntax:

Stream<T> filter(Predicate<? super T> predicate)

Where Stream is an interface and T is the type of the input to the predicate.

Return Type: A new stream.

Implementation:

  1. Filtering the elements to produce a stream divisible by some specific number ranging between 0 to 10.
  2. Filtering the elements to produce a stream with an upperCase letter at any specific index.
  3. Filtering the elements to produce a stream ending with custom alphabetical letters.

Example 1: filter() method with the operation of filtering out the elements divisible by 5.

Java
// Java Program to get a Stream Consisting of the Elements
// of Stream that Matches Given Predicate for Stream filter
// (Predicate predicate)

// Importing required classes
import java.util.*;

// Class
class GFG {

    // Main driver method
    public static void main(String[] args)
    {

        // Creating a list of Integers
        List<Integer> list = Arrays.asList(3, 4, 6, 12, 20);

        // Getting a stream consisting of the
        // elements that are divisible by 5
        // Using Stream filter(Predicate predicate)
        list.stream()
            .filter(num -> num % 5 == 0)
            .forEach(System.out::println);
    }
}

Output
20

Example 2: filter() method with the operation of picking the elements with an upperCase letter at index 1. 

Java
// Java Program to Get Stream Consisting of Elements
// of Stream that Matches Given Predicate
// for Stream Filter (Predicate predicate)

// Importing required classes
import java.util.stream.Stream;

// Class
class GFG {

    // Main driver method
    public static void main(String[] args)
    {
        // Creating a stream of strings
        Stream<String> stream = Stream.of(
            "Geeks", "fOr", "GEEKSQUIZ", "GeeksforGeeks");

        // Getting a stream consisting of the
        // elements having UpperCase Character
        // at custom index say be it '1'
        // using Stream filter(Predicate predicate)
        stream
            .filter(
                str -> Character.isUpperCase(str.charAt(1)))
            .forEach(System.out::println);
    }
}

Output
fOr
GEEKSQUIZ

Example 3: filter() method with the operation picking the elements ending with custom alphabetically letter say it be ‘s’ for implementation purposes. 

Java
// Java Program to Get a Stream Consisting ofElements
//  of Stream that Matches Given predicate
// for Stream filter (Predicate predicate)

// Importing required classes
import java.util.stream.Stream;

// Class
class GFG {

    // Main driver method
    public static void main(String[] args)
    {

        // Creating a stream of strings
        Stream<String> stream = Stream.of(
            "Geeks", "foR", "GeEksQuiz", "GeeksforGeeks");

        // Getting a stream consisting of the
        // elements ending with 's'
        // using Stream filter(Predicate predicate)
        stream.filter(str -> str.endsWith("s"))
            .forEach(System.out::println);
    }
}

Output
Geeks
GeeksforGeeks


Next Article

Similar Reads