Java Stream filter

Java Stream filter tutorial shows how to filter Java streams using filtering operations.

Java Stream

Java Stream is a sequence of elements from a source that supports aggregate operations. Streams do not store elements; the elements are computed on demand. Elements are consumed from data sources such as collections, arrays, or I/O resources.

Stream aggregate operations are similar to SQL operations. We can apply filtering, mapping, reducing, matching, searching, or sorting operations on streams. Streams allow chaining of multiple stream operations. Unlike collections which use external iterations, streams are iterated internally.

Java Stream filter

Java Stream filter method is an intermediate operation, which returns elements of the stream that match the given predicate. A predicate is a function that returns a boolean value.

Java Stream filter string length

The following example filters a list of strings.

JavaStreamFilterStringLength.java
package com.zetcode;

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

public class JavaStreamFilterStringLength {

    public static void main(String[] args) {

        List<String> words = Arrays.asList("pen", "custom", "orphanage",
                "forest", "bubble", "butterfly");

        List<String> result = words.stream().filter(word -> word.length() > 5)
                .collect(Collectors.toList());

        result.forEach(word -> System.out.println(word));
    }
}

We have a list of words. We filter the list to include only strings whose length is bigger than five.

List<String> result = words.stream().filter(word -> word.length() > 5)
        .collect(Collectors.toList());

With the stream() method, we create a Java Stream from a list of strings. On this stream, we apply the filter() method. The filter() method accepts an anonymous functions that returns a boolean true for all elements of the stream whose length is bigger that five. We create a list back from the stream with the collect() method.

result.forEach(word -> System.out.println(word));

We go through the result with the forEach() method and print all its elements to the console.

custom
orphanage
forest
bubble
butterfly

These words have more that five characters.

Java Stream filter null values

The next example filters out null values.

JavaStreamFilterRemoveNulls.java
package com.zetcode;

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

public class JavaStreamFilterRemoveNulls {

    public static void main(String[] args) {

        List<String> words = Arrays.asList("cup", null, "forest",
                "sky", "book", null, "theatre");

        List<String> result = words.stream().filter(w -> w != null)
                .collect(Collectors.toList());

        System.out.println(result);
    }
}

We have a list of words. With the Stream filtering operation, we create a new list with null values removed.

List<String> result = words.stream().filter(w -> w != null)
        .collect(Collectors.toList());

In the body of the lambda expression, we check that the value is not null. The collect() method is a terminal operation that creates a list from the filtered stream.

[cup, forest, sky, book, theatre]

There are no null values in the final output.

Java Stream multiple filter operations

It is possible to apply multiple filter operations on a stream.

JavaStreamMultipleFilters.java
package com.zetcode;

import java.util.Arrays;
import java.util.function.IntConsumer;

public class JavaStreamMultipleFilters {

    public static void main(String[] args) {

        int[] inums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
        
        IntConsumer icons = i -> System.out.print(i + " ");
        
        Arrays.stream(inums).filter(e -> e < 6 || e > 10)
                .filter(e -> e % 2 == 0).forEach(icons);
    }
}

In the example, we apply multiple filter operations on a stream of integers.

int[] inums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

We have an array of integer values.

IntConsumer icons = i -> System.out.print(i + " ");

IntConsumer is an operation that accepts a single integer value argument and returns no result.

Arrays.stream(inums).filter(e -> e < 6 || e > 10)
        .filter(e -> e % 2 == 0).forEach(icons);

A stream is created from the array with the Arrays.stream() method. Multiple filtering operations are performed.

2 4 12 14 

These integers fulfill all the filtering conditions.

Java Stream filter objects

The next example shows how to filter objects.

User.java
package com.zetcode;

public class User {

    private String name;
    private String email;

    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

In the example, we use this User class.

JavaStreamFilterObjects.java
package com.zetcode;

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

public class JavaStreamFilterObjects {
    
    public static void main(String[] args) {

        List<User> persons = Arrays.asList(
                new User("Jack", "jack234@gmail.com"),
                new User("Peter", "pete2@post.com"),
                new User("Lucy", "lucy17@gmail.com"),
                new User("Robert", "bob56@post.com"),
                new User("Martin", "mato4@imail.com")
        );
        
        List<User> result = persons.stream()
                .filter(person -> person.getEmail().matches(".*post\\.com"))
                .collect(Collectors.toList());
        
        result.forEach(p -> System.out.println(p.getName()));
    }
}

The example creates a stream of User objects. It filters those which match a specific regular expression.

List<User> result = persons.stream()
        .filter(person -> person.getEmail().matches(".*post\\.com"))
        .collect(Collectors.toList());

In the filter predicate, we choose emails that match the .*post\\.com pattern.

Peter
Robert

These two users have emails matching the regular pattern.

Java Stream filter map by keys

In the following example, we filter a map by its keys.

JavaStreamFilterMapByValues.java
package com.zetcode;

import java.util.HashMap;
import java.util.Map;

public class JavaStreamFilterMapByKeys {

    public static void main(String[] args) {

        Map<String, String> hmap = new HashMap<>();
        
        hmap.put("de", "Germany");
        hmap.put("hu", "Hungary");
        hmap.put("sk", "Slovakia");
        hmap.put("si", "Slovenia");
        hmap.put("so", "Somalia");
        hmap.put("us", "United States");
        hmap.put("ru", "Russia");
        
        hmap.entrySet().stream().filter(map -> map.getKey().startsWith("s"))
                .forEach(m -> System.out.println(m));
    }
}

The example filters domain names starting with s letter.

si=Slovenia
sk=Slovakia
so=Somalia

This is the output of the example.

Java Stream filter map by values

In the following example, we filter a map by its values.

JavaStreamFilterMapByValues.java
package com.zetcode;

import java.util.HashMap;
import java.util.Map;

public class JavaStreamFilterMapByValues {

    public static void main(String[] args) {

        Map<String, String> hmap = new HashMap<>();

        hmap.put("de", "Germany");
        hmap.put("hu", "Hungary");
        hmap.put("sk", "Slovakia");
        hmap.put("si", "Slovenia");
        hmap.put("so", "Somalia");
        hmap.put("us", "United States");
        hmap.put("ru", "Russia");

        hmap.entrySet().stream().filter(map -> map.getValue().equals("Slovakia")
                || map.getValue().equals("Slovenia"))
                .forEach(m -> System.out.println(m));
    }
}

In the example, we filter out two countries from the map.

si=Slovenia
sk=Slovakia

This is the output.

In this tutorial, we have have worked with Java Stream filtering operations. You might also be interested in the related tutorials: Java tutorial, HashMap iteration in Java, Java ArrayList tutorial, Java HashSet tutorial, Java NumberFormat tutorial, Java 8 forEach tutorial, Filtering a list in Java, and Reading and writing ICO images in Java.