Java Stream Filter

JavaJavaBeginner
Practice Now

Introduction

Java Streams provide us with a way to process data in an efficient and concise manner. The filter() method of Streams allows us to filter out elements from a given stream based on a condition or a predicate. In this lab, we will learn how to use the filter() method and filter elements from a stream in Java.

Create a Stream of Integers

Let's create a Stream of Integers and filter out the even numbers from it. We will declare a list of integers, convert it into a Stream, and then use the filter() method to filter out the even numbers.

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

public class StreamFilter {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        Stream<Integer> stream = numbers.stream();

        Stream<Integer> evenNumbers = stream.filter(i -> i % 2 == 0);

        List<Integer> result = evenNumbers.collect(Collectors.toList());

        System.out.println(result); // Output: [2, 4, 6, 8, 10]
    }
}

In the above code, we declare a list of integers, convert it into a Stream using the stream() method, and then filter out the even numbers using the filter() method.

Create a Stream of Strings

Let's create a Stream of Strings and filter out the strings containing a specific letter from it. We will declare a list of strings, convert it into a Stream, and then use the filter() method to filter out the strings containing a specific letter.

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

public class StreamFilter {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("hello", "world", "java", "stream", "filter");
        Stream<String> stream = words.stream();

        char letter = 'l';

        Stream<String> filteredWords = stream.filter(s -> s.indexOf(letter) != -1);

        List<String> result = filteredWords.collect(Collectors.toList());

        System.out.println(result); // Output: [hello, world, filter]
    }
}

In the above code, we declare a list of strings, convert it into a Stream using the stream() method, and then filter out the strings containing the letter 'l' using the filter() method.

Create a Stream of Custom Objects

Let's create a Stream of custom objects and filter out the objects based on a condition. We will declare a list of custom objects, convert it into a Stream, and then use the filter() method to filter out the objects based on a condition.

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

public class StreamFilter {
    public static void main(String[] args) {
        List<Person> people = Arrays.asList(
                new Person("John", 25),
                new Person("Sarah", 30),
                new Person("Mike", 35),
                new Person("Emma", 40)
        );

        Stream<Person> stream = people.stream();

        Stream<Person> filteredPeople = stream.filter(p -> p.getAge() > 30);

        List<Person> result = filteredPeople.collect(Collectors.toList());

        System.out.println(result); // Output: [Mike{name='Mike', age=35}, Emma{name='Emma', age=40}]
    }
}

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;
    }

    @Override
    public String toString() {
        return name + "{name='" + name + '\'' + ", age=" + age + '}';
    }
}

In the above code, we declare a list of custom objects of type Person, convert it into a Stream using the stream() method, and then filter out the objects whose age is greater than 30 using the filter() method.

Summary

In this lab, we learned how to use the filter() method of Java Streams to filter out elements based on a condition or a predicate. We worked on filtering streams of integers, strings, and custom objects using the filter() method.

Other Java Tutorials you may like