Open In App

Java Stream API – Filters

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn Java Stream Filter API. We will cover,

1. How stream filter API works.

2. Filter by Object Properties.

3. Filter by Index.

4. Filter by custom Object properties.

Stream Filter API

Filter API takes a Predicate. The predicate is a Functional Interface. It takes an argument of any type and returns Boolean. The element will be considered for the next API in the pipeline if the function returns true. Otherwise, the element is filtered out.


Output
https://www.geeksforgeeks.org/

Filter by Object Properties

Filter by Object properties uses java operators. The below example explains how to filter by properties of an object.


Output
4
10
======
stream
sequence
======
am
old
and
in

Filter by Object Indices

Filtering by indexes can be done in two ways.

1. Atomic Integers

We need to use AtomicInteger because predicates expect final variables as parameters. As long as filter function(Predicate) returns boolean we can use any expression. Here, getAndIncrement() method of AtomicInteger increments the current value by 1 and returns final int value.


Output
stream
a
of
like

2. Intstream Approach

We can use Intstream and map the array elements based on the index. Here first we create an Intstream of a range of numbers. Check if a number is even, then overwrite/map the integer with the array element.


Output
stream
a
of
like

Filter by Custom Properties

We can use any Java Object property for filtering. Here we are filtering by age.


Output
Employee [name=Kumar]
Employee [name=Rakesh]

We can also create a custom function for filtering. The function must take a parameter and return a boolean value.


Output
madam
refer
racecar


Article Tags :
Practice Tags :

Similar Reads