Java Stream Filter List of Objects

To filter a list of objects using Java streams, you can use the filter() method along with a predicate that specifies the filtering condition.

Example 1: Java Stream Filter List of Person Objects By Age

In this example, we have a Person class with name and age attributes. We create a list of Person objects and then use the stream() method to convert it into a stream. We apply the filter() method on the stream, providing a lambda expression as the predicate. In this case, we filter the people who are older than 25.
import java.util.ArrayList;
import java.util.List;

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

public class Main {
    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();
        people.add(new Person("Alice", 25));
        people.add(new Person("Bob", 30));
        people.add(new Person("Charlie", 20));

        // Filter people who are older than 25
        List<Person> filteredPeople = people.stream()
                .filter(person -> person.getAge() > 25)
                .toList();

        // Print the filtered people
        filteredPeople.forEach(person -> System.out.println(person.getName()));
    }
}

Output:

Bob

Example 2: Filtering by a String property (Filter Products by category)

In this example, we create a list of Product objects and filter them based on the category property. We use the filter() method with a lambda expression that checks if the category equals "Electronics". The filtered products are collected into a new list, and their names are printed.
import java.util.ArrayList;
import java.util.List;

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 class Main {
    public static void main(String[] args) {
        List<Product> products = new ArrayList<>();
        products.add(new Product("Phone", 999.99, "Electronics"));
        products.add(new Product("Shirt", 29.99, "Clothing"));
        products.add(new Product("TV", 1499.99, "Electronics"));
        products.add(new Product("Jeans", 59.99, "Clothing"));

        // Filter products belonging to the "Electronics" category
        List<Product> filteredProducts = products.stream()
                .filter(product -> product.getCategory().equals("Electronics"))
                .toList();

        // Print the filtered products
        filteredProducts.forEach(product -> System.out.println(product.getName()));
    }
}

Output:

Phone
TV

Example 3: Filtering by a Numeric property (Filter Cars by price)

In this example, we have a list of Car objects and want to filter them based on a maximum price. We use the filter() method with a lambda expression that checks if the car's price is less than or equal to the maxPrice threshold.
import java.util.ArrayList;
import java.util.List;

class Car {
    private String make;
    private String model;
    private double price;

    public Car(String make, String model, double price) {
        this.make = make;
        this.model = model;
        this.price = price;
    }

    public String getMake() {
        return make;
    }

    public String getModel() {
        return model;
    }

    public double getPrice() {
        return price;
    }
}

public class Main {
    public static void main(String[] args) {
        List<Car> cars = new ArrayList<>();
        cars.add(new Car("Toyota", "Camry", 25000));
        cars.add(new Car("Honda", "Civic", 22000));
        cars.add(new Car("BMW", "X5", 65000));
        cars.add(new Car("Ford", "Mustang", 40000));

        double maxPrice = 30000;

        // Filter cars with price less than or equal to maxPrice
        List<Car> filteredCars = cars.stream()
                .filter(car -> car.getPrice() <= maxPrice)
                .toList();

        // Print the filtered cars
        filteredCars.forEach(car -> System.out.println(car.getMake() + " " + car.getModel()));
    }
}

Output:

Toyota Camry
Honda Civic

Comments