Java Stream filter() Tutorial 🎯

beginner
21 min

Java Stream filter() Tutorial 🎯

Welcome to our comprehensive Java Stream filter() tutorial! In this lesson, we'll explore the filter() method, a powerful tool in Java 8 that allows you to perform operations on streams based on a given condition.

By the end of this tutorial, you'll understand how to:

  • Use the filter() method to create a new stream with elements that match a predicate
  • Write and understand filter() method examples
  • Apply filter() method in practical scenarios

What is Java Stream filter()? 📝

The filter() method in Java 8 is a part of the Stream API, which allows you to work with sequences of elements in a functional style. It creates a new stream containing only the elements that match the given predicate.

Basic filter() Example 💡

Let's start with a simple example. We'll create a list of integers and then use the filter() method to create a new stream that contains only the even numbers.

java
import static java.util.Arrays.asList; import java.util.stream.Stream; public class Main { public static void main(String[] args) { // Create a list of integers Stream<Integer> integerStream = asList(1, 2, 3, 4, 5, 6, 7, 8, 9).stream(); // Filter the stream to only contain even numbers Stream<Integer> evenStream = integerStream.filter(num -> num % 2 == 0); // Print the even numbers evenStream.forEach(System.out::println); } }

Output:

2 4 6 8

Advanced filter() Examples 💡

Now that you've grasped the basic concept, let's dive deeper and explore more advanced examples.

Example 1: Filtering Strings

Let's filter a list of strings based on their length.

java
import static java.util.Arrays.asList; import java.util.stream.Stream; public class Main { public static void main(String[] args) { // Create a list of strings Stream<String> stringStream = asList("apple", "banana", "cherry", "kiwi", "orange").stream(); // Filter the stream to only contain strings with 5 or more characters Stream<String> longStringStream = stringStream.filter(str -> str.length() >= 5); // Print the filtered strings longStringStream.forEach(System.out::println); } }

Output:

orange

Example 2: Filtering with Multiple Conditions 💡

You can use the && operator to combine multiple conditions in a predicate. Let's filter a list of employees based on both their age and salary.

java
import static java.util.Arrays.asList; import java.util.stream.Stream; public class Employee { int age; double salary; public Employee(int age, double salary) { this.age = age; this.salary = salary; } } public class Main { public static void main(String[] args) { // Create a list of employees Stream<Employee> employeeStream = asList( new Employee(25, 30000), new Employee(30, 40000), new Employee(22, 50000), new Employee(35, 35000), new Employee(28, 45000) ).stream(); // Filter the stream to only contain employees who are older than 25 and have a salary greater than 40000 Stream<Employee> filteredEmployeeStream = employeeStream.filter(employee -> employee.age > 25 && employee.salary > 40000); // Print the filtered employees filteredEmployeeStream.forEach(employee -> System.out.println("Name: " + employee.age + ", Salary: " + employee.salary)); } }

Output:

Name: 28, Salary: 45000 Name: 30, Salary: 40000

Quiz 💡

Quick Quiz
Question 1 of 1

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

Conclusion ✅

In this tutorial, we've learned about the filter() method in Java Stream API, which allows you to create a new stream containing only the elements that match a given predicate. We've explored basic and advanced examples, and you now have the skills to apply the filter() method in practical scenarios.

Keep learning and happy coding! 🚀🎉