-
Notifications
You must be signed in to change notification settings - Fork 0
/
_Map.java
53 lines (43 loc) · 1.53 KB
/
_Map.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
package com.umer.javafunctional.streams;
import java.util.Arrays;
import java.util.List;
import com.umer.javafunctional.streams._Stream.Gender;
import com.umer.javafunctional.streams._Stream.Person;
import static java.util.stream.Collectors.*;
public class _Map {
private static final String EMPTY_STRING = " ";
public static List<Person> createPeople(){
return Arrays.asList(
new Person("Bob", Gender.MALE),
new Person("Alice", Gender.FEMALE),
new Person("Sara", Gender.FEMALE),
new Person("Claire", Gender.FEMALE),
new Person("Tom", Gender.MALE),
new Person("John", Gender.MALE),
new Person("Smith", Gender.MALE)
);
}
public static void main(String[] args) {
List<Person> people=createPeople();
// create a map with name as key, and person as value.
System.out.println(EMPTY_STRING);
System.out.println("--------------------------------------");
System.out.println("create a map with name as key, and person as value.");
System.out.println(
people.stream()
.collect(toMap(
person -> person.getName(),
person -> person)
));
System.out.println("--------------------------------------");
System.out.println(EMPTY_STRING);
System.out.println(EMPTY_STRING);
System.out.println("--------------------------------------");
System.out.println("create a map and group by gender.");
System.out.println(
people.stream()
.collect(groupingBy(Person::getGender)));
System.out.println("--------------------------------------");
System.out.println(EMPTY_STRING);
}
}