-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamsAPI.java
121 lines (97 loc) · 3.15 KB
/
StreamsAPI.java
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
package ds;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import ds.StreamsAPI.Gender;
class Student {
private final String name;
private final int age;
private final Gender gender;
public int getAge() {
return age;
}
public Gender getGender() {
return gender;
}
public Student(String name, int age, Gender gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public String toString() {
return "Student: name="+name+" age="+age+" gender="+gender;
}
}
public class StreamsAPI {
private static List<Student> getStudent() {
return List.of(
new Student("Amit", 20, Gender.MALE),
new Student("Trevor", 33, Gender.FEMALE),
new Student("Joshua", 27, Gender.MALE),
new Student("Ali", 39, Gender.MALE),
new Student("Dayle", 14, Gender.FEMALE),
new Student("Priyanka", 39, Gender.MALE),
new Student("Heidi", 17, Gender.FEMALE),
new Student("Fatima", 90, Gender.FEMALE)
);
}
public enum Gender {MALE, FEMALE}; //0-MALE, 1- FEMALE
public static void main(String []args) {
List<Student> students= getStudent();
// Imperative approach
// List<Student> females= new ArrayList<>();
// for(Student student:students) {
// if(student.getGender().equals(Gender.FEMALE)) {
// females.add(student);
// }
// }
// females.forEach(System.out::println);
// Declarative approach
//Filter
List<Student> females= students.stream()
.filter(student -> student.getGender().equals(Gender.FEMALE))
.collect(Collectors.toList());
//females.forEach(System.out::println);
//Sort
List<Student> student= students.stream()
.sorted(Comparator.comparing(Student::getAge).thenComparing(Comparator.comparing(Student::getName).reversed()))
.collect(Collectors.toList());
//student.forEach(System.out::println);
//All match
// System.out.println(students.stream()
// .allMatch(s->s.getAge()>14));
//Any Match
// System.out.println(students.stream()
// .anyMatch(s->s.getAge()>59));
// None match
// System.out.println(students.stream()
// .noneMatch(s->s.getName().equals("Ali")));
//Max
// students.stream()
// .max(Comparator.comparing(Student::getAge))
// .ifPresent(System.out::println);
//Min
// students.stream()
// .min(Comparator.comparing(Student::getAge))
// .ifPresent(System.out::println);
//group by
Map<Gender, List<Student>> s=students.stream()
.collect(Collectors.groupingBy(Student::getGender));
// s.forEach((gender, students1)->{
// System.out.println(gender);
// students1.forEach(System.out::println);
// System.out.println();
// });
Optional<String> youngestMale= students.stream()
.filter(s1 -> s1.getGender().equals(Gender.MALE))
.min(Comparator.comparing(Student::getAge))
.map(Student::getName);
youngestMale.ifPresent(System.out::println);
}
}