Stream filter() in Java with examples

03 May, 2022
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:
Filtering out the elements divisible by some specific number ranging between 0 to 10.Filtering out the elements with an upperCase letter at any specific index.Filtering out the elements ending with custom alphabetical letters.
Filtering out the elements divisible by some specific number ranging between 0 to 10.
Filtering out the elements with an upperCase letter at any specific index.
Filtering out the elements 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 classesimport java.util.*;  // Classclass 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);    }}
20
Example 2: filter() method with the operation of filtering out 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 classesimport java.util.stream.Stream;  // Classclass 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);    }}
fOr
GEEKSQUIZ
Example 3: filter() method with the operation of filtering out 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 classesimport java.util.stream.Stream;  // Classclass 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);    }}
Geeks
GeeksforGeeks
solankimayank
Java - util package
Java-Functions
java-stream
Java-Stream interface
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.