-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrouping.java
More file actions
44 lines (40 loc) · 1.26 KB
/
Grouping.java
File metadata and controls
44 lines (40 loc) · 1.26 KB
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
// LDE-Grouping/Grouping.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Grouping {
public static void main (String[] args) {
List<Person> list = Arrays.asList(
new Person("John", "UK"),
new Person("Mary", "US"),
new Person("Xue", "CH"),
new Person("Kate", "UK"),
new Person("Janek", "PL"),
new Person("Cindy", "US"),
new Person("Bao", "CH"),
new Person("Kasia", "PL")
);
// collect gives Map<String,List<Person>>
// groupingBy expects Function...
list
.stream()
.collect(Collectors.groupingBy(Person::getCountry))
.entrySet()
.stream()
.forEach(e -> System.out.println(e.getKey() +
" -> " + e.getValue()));
}
}
class Person {
private final String name;
private final String country;
public Person(String n, String c) {
name = n; country = c;
}
public String getName() { return name; }
public String getCountry() { return country; }
@Override
public String toString() {
return name + " (" + country + ")";
}
}