Java Stream Filter Multiple Conditions

In this blog post, we will explore how to use Java Stream's filter() method with multiple conditions.

Java Stream API provides powerful tools for processing and manipulating collections of objects in a concise and functional way. One of the most commonly used operations in Java Stream is the filter method, which allows us to selectively include or exclude elements from a stream based on a specified condition. While filtering based on a single condition is straightforward, there are situations where we need to apply multiple conditions to filter the data effectively. 

To filter a stream with multiple conditions, we can use lambda expressions or method references to define complex filtering predicates. These predicates can combine multiple conditions using logical operators such as && (AND) or || (OR).

Example 1: Filtering products based on price and category 

In this example, we filter the stream of Product objects by checking if the category is "Electronics" and the price is greater than $500.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Product {
    private String name;
    private double price;
    private String category;

    public Product(String name, double price, String category) {
        this.name = name;
        this.price = price;
        this.category = category;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public String getCategory() {
        return category;
    }

    public static void main(String[] args) {
        List<Product> products = Arrays.asList(
                new Product("Laptop", 1200.0, "Electronics"),
                new Product("Smartphone", 800.0, "Electronics"),
                new Product("Shirt", 30.0, "Fashion"),
                new Product("TV", 900.0, "Electronics")
        );

        List<Product> filteredProducts = products.stream()
                .filter(product -> product.getCategory().equals("Electronics") && product.getPrice() > 500.0)
                .collect(Collectors.toList());

        filteredProducts.forEach(product -> System.out.println(product.getName()));
    }
}

Output:

Laptop
Smartphone
TV

Example 2: Filtering employees based on department and salary range

In this example, we filter the stream of Employee objects based on multiple conditions: department being "Sales" and salary falling between $3000 and $5000.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Employee {
    private String name;
    private String department;
    private double salary;

    public Employee(String name, String department, double salary) {
        this.name = name;
        this.department = department;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public String getDepartment() {
        return department;
    }

    public double getSalary() {
        return salary;
    }

    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
                new Employee("John", "Sales", 4000.0),
                new Employee("Alice", "Marketing", 4500.0),
                new Employee("Bob", "Sales", 5500.0),
                new Employee("Jane", "HR", 3000.0)
        );

        List<Employee> filteredEmployees = employees.stream()
                .filter(emp -> emp.getDepartment().equals("Sales") && emp.getSalary() >= 3000.0 && emp.getSalary() <= 5000.0)
                .collect(Collectors.toList());

        filteredEmployees.forEach(emp -> System.out.println(emp.getName()));
    }
}

Output:

John

Comments