Saturday , August 19 2017
Home / java8 / Java 8 Stream Filter Example with Objects

Java 8 Stream Filter Example with Objects

In this tutorials, 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 Stream interface. Which returns the Stream object consisting 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 equivalent 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 :


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

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)

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 :


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());
    }
}

Java

Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Java 8 Read File Line By Line Example

In this tutorials, I am going to show you how to read a File line …

Leave a Reply

Your email address will not be published. Required fields are marked *