Java Stream Filter

--

In this article, we’ll go through the Java Stream filter() method through detailed examples.

1. Java Stream Filter Method Overview

The stream.filter() method is used to filter an object or a primitive based on the truthness of a boolean. The syntax of this method is the following:

Stream<Object_type_or_primitive> filter(Predicate<? super type_of_stream>)

The predicate could have one of the following syntaxes:

1.1 One-Line Lambda

a_variable -> a_boolean_value

1.2 Method Reference

Object_class_::a_method

In many cases, this can replace the previous syntax and the result will be the same. The method a_method must return a boolean.

1.3 Multiline Lambda

a_variable -> {
lines_of_code;
...
return a_boolean;
}

The above will perform some operations before it returns a boolean. Note that this cannot be replaced by method reference without calling an external method.

2. Java Stream Filter Method Example With Lambda or Method Reference

Let’s say we have a list of article objects, the article record is the following:

private static record Article (int id, String title, String authorName){};

And we have a list of these articles:

Article forLoop = new Article(
1,
"For Loop Java Tutorial",
"Dimitris Tasios"
);

Article interfaceTutorial = new Article(
2,
"Java Interface Tutorial",
"Georgios - Nikolaos Palaiologopoulos"
);

Article generics = new Article(
3,
"SQL LIKE Operator Tutorial",
"Ignis Dei"
);

Article mathRandom = new Article(
4,
"Math.random Java Tutorial",
"Georgios - Nikolaos Palaiologopoulos"
);

Article swiftForLoop = new Article(
5,
"Swift For Loop Tutorial",
"Dimitris Tasios"
);

We want to filter only the articles by author’s name.

private static List<Article> filterByAuthorName(List<Article> articles, String authorName){
return articles.stream()
.filter(article -> article.authorName().equals(author))
.collect(Collectors.toList());
}

The above method takes a list of articles and an author’s name and returns a list containing only the articles with a specific author name. So, if we run the following:

filterByAuthorName(articles, "Georgios - Nikolaos Palaiologopoulos")
.forEach(System.out::println);

It will output:

Article[id=2, title=Java Interface Tutorial, authorName=Georgios - Nikolaos Palaiologopoulos]
Article[id=4, title=Math.random Java Tutorial, authorName=Georgios - Nikolaos Palaiologopoulos]

We should mention that we cannot replace the above predicate with a method reference since the predicate inside the filter should accept only one parameter (the article object) and should return a boolean. We could however create a method inside the record Article like the one shown below:

private List<Article> filterByAuthorIsIgnis(List<Article> articles){
return articles.stream()
.filter(Article::isIgnis)
.collect(Collectors.toList());
}

And then use it in the method below:

private List<Article> filterByAuthorIsIgnis(List<Article> articles){
return articles.stream()
.filter(Article::isIgnis)
.collect(Collectors.toList());
}

As a result, we would get only the articles written by “Ignis Dei”.

3. Java Stream Filter Method Example With Multiple Lines of Code

Furthermore, you can write multiple lines inside a lambda function in the filter method; for example, you might like to create two booleans and return true only if both are true. Of course, you could have written the same with a simple inline lambda (article -> article.title().contains("Java") && article.authorName().equals("Dimitris Tasios")) and the result would be the same (although it might be less readable than the snippet below).

private static List<Article> filterIfContainsJavaAndAuthorTasios(List<Article> articles){
return articles.stream()
.filter(article -> {
boolean containsJava = article.title().contains("Java");
boolean isTasios = article.authorName().equals("Dimitris Tasios");
return containsJava && isTasios;
})
.collect(Collectors.toList());
}

As a result, if we run the following snippet:

filterByAuthorIsIgnis(articles) 
.forEach(System.out::println);

We will get the following output:

Article[id=1, title=For Loop Java Tutorial, authorName=Dimitris Tasios]

4. Conclusion

By now you should know how to use the stream.filter() method of Java Stream API. You can find the source code on our GitHub page.

5. Sources

[1]: Stream (Java SE 12 & JDK 12 ) — Oracle Help Center

--

--

Georgios Nikolaos Palaiologopoulos

Experienced Java Developer | Focused on Backend Software Development with Java & Spring Boot | Technical Writer