Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Filtering Elements with the filter() Method | Intermediate Operations in Stream API
Stream API
course content

Course Content

Stream API

Stream API

1. Fundamentals and Functional Capabilities of Stream API
4. Practical Applications of Stream API

book
Filtering Elements with the filter() Method

When working with data collections in Java, you often need to extract specific elements that meet certain conditions. Traditionally, this requires loops and conditional statements, making the code more verbose. That's where the filter() method comes to the rescue.

You've already used this method in our examples, but we haven't discussed in detail what it takes as an argument. It accepts a functional interface Predicate<T>, which defines whether an element should be included in the resulting stream.

Here, T is the type of elements in the stream, and Predicate<? super T> is the filtering condition, used to check each element.

Practical Application

Let's say you have a list of employees, each with a name and yearsOfExperience in the company. You want to select only those who have been with the company for more than 5 years to offer them a bonus or additional benefits.

java

Main

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
package com.example; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Employee { private final String name; private final int yearsOfExperience; public Employee(String name, int yearsOfExperience) { this.name = name; this.yearsOfExperience = yearsOfExperience; } public String getName() { return name; } public int getYearsOfExperience() { return yearsOfExperience; } @Override public String toString() { return name + " (" + yearsOfExperience + " years)"; } } public class Main { public static void main(String[] args) { List<Employee> employees = Arrays.asList( new Employee("John", 6), new Employee("Sarah", 4), new Employee("Mike", 7), new Employee("Anna", 3), new Employee("Tom", 8) ); // Filtering employees with more than 5 years of experience List<Employee> experiencedEmployees = employees.stream() .filter(e -> e.getYearsOfExperience() > 5) .toList(); System.out.println("Experienced employees: " + experiencedEmployees); } }

In this example, you create a stream of employees using stream(). Then, using the filter method, you filter employees who have more than 5 years of experience.

The result is a new list containing only those employees who meet this condition.

When to Use filter

In most cases, it's better to use filter() at the beginning to reduce the amount of data for subsequent operations. This improves performance since later operations will be performed on a smaller dataset.

Example

If you have a list of employees and you want to first filter those with more than 5 years of experience and then transform the remaining employees into a different format, it makes sense to start with filtering.

As you can see, in this example, you place the filter() before the map() method because it allows you to reduce the number of elements you need to work with first. You filter employees with more than 5 years of experience, and then, extract their names.

This order of operations is efficient because it prevents unnecessary data processing during the transformation phase.

1. What does the filter() method do in Stream API?

2. What type of argument does the filter() method accept?

3. What happens if you use a condition in the filter() method that always returns false?

question mark

What does the filter() method do in Stream API?

Select the correct answer

question mark

What type of argument does the filter() method accept?

Select the correct answer

question mark

What happens if you use a condition in the filter() method that always returns false?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 2
We're sorry to hear that something went wrong. What happened?
some-alt