Java 8 Stream Filter examples

Java 8 Stream Filter examples


Java 8 Stream API is very useful for filtering collections. Lets see example of java 8 filter API
Scenario

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
class TempStudent {
    public String name;
    public int age;
    public Address address;
    public List<MobileNumber> mobileNumbers;
 
    public TempStudent(String name, int age, Address address, List<MobileNumber> mobileNumbers) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.mobileNumbers = mobileNumbers;
    }
}
 
class Student{
    private String name;
    private int age;
    private Address address;
    private List<MobileNumber> mobileNumbers;
 
    public Student(String name, int age, Address address, List<MobileNumber> mobileNumbers) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.mobileNumbers = mobileNumbers;
    }
 
    public String getName() {
        return name;
    }
 
    public int getAge() {
        return age;
    }
 
    public Address getAddress() {
        return address;
    }
 
    public List<MobileNumber> getMobileNumbers() {
        return mobileNumbers;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public void setAddress(Address address) {
        this.address = address;
    }
 
    public void setMobileNumbers(List<MobileNumber> mobileNumbers) {
        this.mobileNumbers = mobileNumbers;
    }
 
    @Override
    public String toString() {
        return "Student{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", address=" + address +
            ", mobileNumbers=" + mobileNumbers +
            '}';
    }
}
 
class Address{
    private String zipcode;
 
    public Address(String zipcode) {
        this.zipcode = zipcode;
    }
 
    public String getZipcode() {
        return zipcode;
    }
 
    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }
}
 
class MobileNumber{
    private String number;
 
    public MobileNumber(String number) {
        this.number = number;
    }
 
    public String getNumber() {
        return number;
    }
 
    public void setNumber(String number) {
        this.number = number;
    }
}

Scenario

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.javabypatel;
 
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
public class StreamTest {
    public static void main(String[] args) {
 
        Student student1 = new Student(
            "Jayesh",
            20,
            new Address("1234"),
            Arrays.asList(new MobileNumber("1233"), new MobileNumber("1234")));
 
        Student student2 = new Student(
            "Khyati",
            20,
            new Address("1235"),
            Arrays.asList(new MobileNumber("1111"), new MobileNumber("3333"), new MobileNumber("1233")));
 
        Student student3 = new Student(
            "Jason",
            20,
            new Address("1236"),
            Arrays.asList(new MobileNumber("3333"), new MobileNumber("4444")));
 
        List<Student> students = Arrays.asList(student1, student2, student3);
 
        /*****************************************************
         Get student with exact match name "jayesh"
        *****************************************************/
        Optional<Student> stud = students.stream()
            .filter(student -> student.getName().equals("Jayesh"))
            .findFirst();
        System.out.println(stud.isPresent() ? stud.get().getName() : "No student found");
        System.out.println("--------------------");
 
        /*****************************************************
         Get student with matching address "1235"
        *****************************************************/
        Optional<Student> stud1 = students.stream()
            .filter(student -> student.getAddress().getZipcode().equals("1235"))
            .findFirst();
        System.out.println(stud1.isPresent() ? stud1.get().getName() : "No student found");
        System.out.println("--------------------");
 
        /*****************************************************
         Get all student having mobile numbers 3333.
        *****************************************************/
        List<Student> stud2 = students.stream()
            .filter(student111 -> student111.getMobileNumbers().stream().anyMatch(x -> Objects.equals(x.getNumber(), "3333")))
            .collect(Collectors.toList());
 
          String result1 = stud2.stream().map(std -> std.getName()).collect(Collectors.joining(",", "[", "]"));
          System.out.println(result1);
        System.out.println("--------------------");
 
        /*****************************************************
         Get all student having mobile number 1233 and 1234
         *****************************************************/
        List<Student> stud3 = students.stream()
            .filter(student -> student.getMobileNumbers().stream().allMatch(x -> Objects.equals(x.getNumber(), "1233") || Objects.equals(x.getNumber(), "1234")))
            .collect(Collectors.toList());
 
        String result4 = stud3.stream().map(std -> std.getName()).collect(Collectors.joining(",", "[", "]"));
        System.out.println(result4);
        System.out.println("--------------------");
 
        /*****************************************************
         Create a List<Student> from the List<TempStudent>
        *****************************************************/
        TempStudent tmpStud1 = new TempStudent(
            "Jayesh1",
            201,
            new Address("12341"),
            Arrays.asList(new MobileNumber("12331"), new MobileNumber("12341")));
 
        TempStudent tmpStud2 = new TempStudent(
            "Khyati1",
            202,
            new Address("12351"),
            Arrays.asList(new MobileNumber("11111"), new MobileNumber("33331"), new MobileNumber("12331")));
 
        List<TempStudent> tmpStudents = Arrays.asList(tmpStud1, tmpStud2);
 
        List<Student> studentList = tmpStudents.stream()
            .map(tmpStud -> new Student(tmpStud.name, tmpStud.age, tmpStud.address, tmpStud.mobileNumbers))
            .collect(Collectors.toList());
 
        System.out.println(studentList);
        System.out.println("--------------------");
 
        /*****************************************************
         Convert List<Student> to List<String> of student name
        *****************************************************/
        List<String> studentsName = studentList.stream()
            .map(Student::getName)
            .collect(Collectors.toList());
 
        System.out.println(studentsName.stream().collect(Collectors.joining(",")));
        System.out.println(studentsName.stream().collect(Collectors.joining(",", "[", "]")));
        System.out.println("--------------------");
 
        /*****************************************************
         Convert List<students> to String
        *****************************************************/
        String name = students.stream()
            .map(Student::getName)
            .collect(Collectors.joining(",", "[", "]"));
        System.out.println(name);
        System.out.println("--------------------");
 
        /*****************************************************
         Change the case of List<String>
        *****************************************************/
        List<String> nameList =
            Arrays.asList("Jayesh", "Dany", "Khyati", "Hello", "Mango");
 
        nameList.stream()
            .map(String::toUpperCase)
            .forEach(System.out::println);
        System.out.println("--------------------");
 
        /*****************************************************
         Sort List<String>
         *****************************************************/
        List<String> namesList =
            Arrays.asList("Jayesh", "Dany", "Khyati", "Hello", "Mango");
 
        namesList.stream()
            .sorted()
            .forEach(System.out::println);
        System.out.println("--------------------");
 
        /*****************************************************
         Conditionally apply Filter condition, say if flag is enabled then
         *****************************************************/
        boolean sortConditionFlag = true;
 
        Stream<Student> conditionalFilterResult = students.stream()
            .filter(std -> std.getName().startsWith("J"));
 
        if(sortConditionFlag){
            conditionalFilterResult = conditionalFilterResult.sorted(Comparator.comparing(Student::getName));
        }
 
        System.out.println("Before sorting :");
        students.forEach(s -> System.out.println(s.getName()));
 
        List<Student> list = conditionalFilterResult.collect(Collectors.toList());
        System.out.println("After filter and conditional sorting :");
        list.forEach(s -> System.out.println(s.getName()));
 
    }
}
Output

Jayesh
--------------------
Khyati
--------------------
[Khyati,Jason]
--------------------
[Jayesh]
--------------------
[Student{name='Jayesh1', age=201, address=com.javabypatel.sea.Address@685cb137, mobileNumbers=[com.javabypatel.sea.MobileNumber@6a41eaa2, com.javabypatel.sea.MobileNumber@7cd62f43]},

Student{name='Khyati1', age=202, address=com.javabypatel.sea.Address@6d4b1c02, mobileNumbers=[com.javabypatel.sea.MobileNumber@6093dd95, com.javabypatel.sea.MobileNumber@5622fdf, com.javabypatel.sea.MobileNumber@4883b407]}]
--------------------
Jayesh1,Khyati1
[Jayesh1,Khyati1]
--------------------
[Jayesh,Khyati,Jason]
--------------------
JAYESH
DANY
KHYATI
HELLO
MANGO
--------------------
Dany
Hello
Jayesh
Khyati
Mango
--------------------
Before sorting :
Jayesh
Khyati
Jason
After filter and conditional sorting :
Jason
Jayesh

You may also like to see


When to use Interface in java with example

Interface Vs Abstract class in Java OOPS.

Top Java Interface Interview Questions and Answers

When to use interface and abstract class in Java? what is the difference between them?

Java Multithreading and Concurrency Interview Questions and Answers with Example

Advanced Multithreading Interview Questions In Java

How ConcurrentHashMap works and ConcurrentHashMap interview questions.

Enjoy !!!! 
If you find any issue in post or face any error while implementing, Please comment.