Java 8 Stream Filter Example | Filter Streams in Java | online TutorialsPoint Streams

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
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.
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);
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
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)
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;
  }
}
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 🙂

Java 8 Read File Line By Line Example
Hibernate Filter Example Xml Configuration
How to convert List to Map in Java 8
How to Filter null values from Java8 Stream
Java 8 How to get common elements from two lists
How to Filter a Map in Java 8
How to Convert Java 8 Stream to Array
Java 8 How to convert Stream to List
How to get Stream count in Java 8
Java 8 Getting Min and Max values from a Stream
How to Convert Iterable to Stream Java 8
How Java 8 Stream generate random String and Numbers
Java8 Concatenate Arrays Example using Stream
Java 8 Stream API and Parallelism
Array of objects in AngularJs  Example

Java 8 Read File Line By Line Example
Hibernate Filter Example Xml Configuration
How to convert List to Map in Java 8
How to Filter null values from Java8 Stream
Java 8 How to get common elements from two lists
How to Filter a Map in Java 8
How to Convert Java 8 Stream to Array
Java 8 How to convert Stream to List
How to get Stream count in Java 8
Java 8 Getting Min and Max values from a Stream
How to Convert Iterable to Stream Java 8
How Java 8 Stream generate random String and Numbers
Java8 Concatenate Arrays Example using Stream
Java 8 Stream API and Parallelism
Array of objects in AngularJs  Example
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String