-
Notifications
You must be signed in to change notification settings - Fork 0
/
_Stream.java
114 lines (80 loc) · 3.4 KB
/
_Stream.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
package com.umer.javafunctional.streams;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.IntConsumer;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;
public class _Stream {
public static void main(String[] args) {
List<Person> people = List.of(
new Person("Umer", Gender.MALE),
new Person("Maria", Gender.FEMALE),
new Person("Aisha", Gender.FEMALE),
new Person("Alex", Gender.MALE),
new Person("Alice", Gender.FEMALE),
new Person("Bob", Gender.PREFER_NOT_TO_SAY)
);
System.out.println("--------------------------------------");
System.out.println("Displaying gender of people");
System.out.println("--------------------------------------");
people.stream()
.map(person -> person.gender)// Converting the Person just to their gender
.collect(Collectors.toSet())// Removing duplicates
.forEach(gender -> System.out.println(gender));// printing the list of gender in the set
System.out.println("--------------------------------------");
System.out.println("Displaying names of people");
System.out.println("--------------------------------------");
people.stream()
.map(person -> person.name)
.collect(Collectors.toSet())
.forEach(name -> System.out.println(name));
System.out.println("--------------------------------------");
System.out.println("Displaying length of each name in order of the input people list");
System.out.println("--------------------------------------");
final Function<? super Person, ? extends String> function = person -> person.name;
ToIntFunction<String> lengthOfName =name -> name.length();
final IntConsumer intConsumer = length -> System.out.println(length);
people.stream()
.map(function)
.mapToInt(lengthOfName)
.forEach(intConsumer);
System.out.println("--------------------------------------");
System.out.println("Displaying the availability of females from the list of people");
System.out.println("--------------------------------------");
final Predicate<? super Person> predicate = person -> Gender.FEMALE.equals(person.gender);
final boolean containsOnlyFemales = people.stream()
.allMatch(predicate);
System.out.println("Does people list contains only females: "+containsOnlyFemales);
final boolean containsAnyFemales = people.stream()
.anyMatch(predicate);
System.out.println("Does people list contains any females: "+containsAnyFemales);
final Predicate<? super Person> PreferNotToSayPredicate = person -> Gender.PREFER_NOT_TO_SAY.equals(person.gender);
final boolean containsAnyoneWhoseGenderIsNotKnown = people.stream()
.noneMatch(PreferNotToSayPredicate);
System.out.println("Does people list contains all, whos gender is specified as male or female: "+containsAnyoneWhoseGenderIsNotKnown);
}
static class Person {
private final String name;
private final Gender gender;
public Person(String name, Gender gender) {
this.name = name;
this.gender = gender;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Person {" + "name='" + name + "\'" + ", gender=" + gender + "}";
}
public String getName() {
return this.name;
}
public Gender getGender() {
return this.gender;
}
}
enum Gender {
MALE, FEMALE, PREFER_NOT_TO_SAY;
}
}