In this tutorial, we are going to show you how to filter a stream of objects in Java 8. We can filter the string values and as well as objects using the Java 8 Stream filter.

Java 8 Stream Filter :

The filter() in Java 8 is a method which is coming from the Stream interface, which returns the Stream object consisting of the elements of this stream that matches the given Predicate(action).

Legacy way of filtering the list in Java :

List<String> list = new ArrayList<String>();
        list.add("Java");
        list.add("Spring");
        list.add("Hibernate");
        list.add("Java8 Filter");
        list.add("JQuery");
        list.add("Hibernate 4");
        list.add("AngularJs");
        list.add("Java8 Streams");
for (String sub : list) {
     if (sub.contains("Java8")) {
           System.out.println(sub);
     }
}

The similar way of filtering the list of strings using Java 8 Stream Filter.

List<String> subjects = list.stream()
                .filter(sub -> sub.contains("Hibernate"))
                .collect(Collectors.toList());

        subjects.forEach(System.out::println);

Stream Filter with Strings :

Java8_Streams_filter.java
package com.onlinetutorialspoint.java8;

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

public class Java8_Streams_filter {
  public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    list.add("Java");
    list.add("Spring");
    list.add("Hibernate");
    list.add("Java8 Filter");
    list.add("JQuery");
    list.add("Hibernate 4");
    list.add("AngularJs");
    list.add("Java8 Streams");
    java8_filter(list);
  }

  public static void normal_filter(List<String> list) {
    for (String sub : list) {
      if (sub.contains("Java8")) {
        System.out.println(sub);
      }
    }
  }

  public static void java8_filter(List<String> list) {
    List<String> subjects = list.stream().filter(sub -> sub.contains("Hibernate")).collect(Collectors.toList());
    subjects.forEach(System.out::println);
  }
}

Recommended: How to use Java8 foreach
Output:

Hibernate
Hibernate 4

Stream Filter with Objects :

We can filter the list of user-defined objects using the Stream filter in Java. Here I am going to create a Subject POJO class to filter the elements.

Student.java (POJO)

Subject.java
class Subject {
  private int subId;
  private String subName;

  public Subject(int subId, String subName) {
    super();
    this.subId = subId;
    this.subName = subName;
  }

  public int getSubId() {
    return subId;
  }

  public String getSubName() {
    return subName;
  }
}

Stream Filter with Objects Example :

Java8_Streams_filter.java
package com.onlinetutorialspoint.java8;

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

public class Java8_Streams_filter {
  public static void main(String[] args) {
    List<Subject> subjects = Arrays.asList(new Subject(1, "Java"), new Subject(2, "Spring"),
        new Subject(3, "Hiberate"));
    java8_filterObjects(subjects);
  }

  public static void java8_filterObjects(List<Subject> subjects) {
    Subject subject = subjects.stream().filter(name -> "Java".equals(name.getSubName())).findAny().orElse(null);
    System.out.println(subject.getSubName());
  }
}

Output:

Java

Happy Learning 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment