Java Stream Filter Tutorial


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

1. Java Stream Map 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


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

1.3 Multiline Lambda

a_variable -> {
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(
        "For Loop Java Tutorial",
        "Dimitris Tasios"

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

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

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

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

Now let’s say we’d like to filter only the articles by author’s name.

private static List<Article> filterByAuthorName(List<Article> articles, String authorName){
                   .filter(article -> article.authorName().equals(author))

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

filterByAuthorName(articles, "Georgios - Nikolaos Palaiologopoulos")

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]

Here 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 static boolean isIgnis(Article article){
    return article.authorName().equals("Ignis Dei");

And then use it in the method below:

private List<Article> filterByAuthorIsIgnis(List<Article> articles){

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 exactly 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){
            .filter(article -> {
               boolean containsJava = article.title().contains("Java");
               boolean isTasios = article.authorName().equals("Dimitris Tasios");
               return containsJava && isTasios;

As a result, if we run the following snippet:


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

Related Posts