-
Notifications
You must be signed in to change notification settings - Fork 27
/
Grouping.java
34 lines (24 loc) · 1 KB
/
Grouping.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
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.*;
public class Grouping {
public static class Dragon {
Dragon(String name) {this.name = name;}
String name;
public String getName() {return name; }
public String toString() {return name; }
public boolean isGreen() {return Math.random() > 0.5;}
}
public static List<Dragon> getDragons() {
return Arrays.asList(new Dragon("Smaug"), new Dragon("Norbert"), new Dragon("Smoochy"), new Dragon("Nuvi"));
}
public static void main(String...args) {
List<Dragon> dragons = getDragons();
Map<Character,List<Dragon>> map = dragons.parallelStream()
.collect(groupingByConcurrent(dragon -> dragon.getName().charAt(0)));
System.out.println(map);
Map<Boolean,List<Dragon>> map2 = dragons.stream()
.collect(partitioningBy(Dragon::isGreen));
System.out.println(map2);
}
}