Skip to content

Commit e2a4591

Browse files
committed
Add demo for groupBy
1 parent f4003f3 commit e2a4591

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package fj.demo;
2+
3+
import fj.Function;
4+
import fj.data.HashMap;
5+
import fj.data.List;
6+
7+
import static fj.data.List.list;
8+
9+
public final class List_groupBy {
10+
11+
public static void main(final String... args) {
12+
keyDemo();
13+
keyValueDemo();
14+
keyValueAccDemo();
15+
}
16+
17+
private static void keyDemo() {
18+
System.out.println("KeyDemo");
19+
final List<String> words = list("Hello", "World", "how", "are", "your", "doing");
20+
final HashMap<Integer, List<String>> lengthMap = words.groupBy(String::length);
21+
22+
lengthMap.foreach(entry -> {
23+
System.out.println(String.format("Words with %d chars: %s", entry._1(), entry._2()));
24+
});
25+
}
26+
27+
private static void keyValueDemo() {
28+
System.out.println("KeyValueDemo");
29+
final List<Integer> xs = list(1, 2, 3, 4, 5, 6, 7, 8, 9);
30+
final HashMap<Integer, List<String>> result = xs.groupBy(x -> x % 3, Integer::toBinaryString);
31+
32+
result.foreach(entry -> {
33+
System.out.println(String.format("Numbers with reminder %d are %s", entry._1(), entry._2()));
34+
});
35+
}
36+
37+
private static void keyValueAccDemo() {
38+
System.out.println("KeyValueAccDemo");
39+
final List<String> words = list("Hello", "World", "how", "are", "your", "doing");
40+
final HashMap<Integer, Integer> lengthCounts =
41+
words.groupBy(String::length, Function.identity(), 0, (word, sum) -> sum + 1);
42+
43+
lengthCounts.foreach(entry -> {
44+
System.out.println(String.format("Words with %d chars: %s", entry._1(), entry._2()));
45+
});
46+
}
47+
}

0 commit comments

Comments
 (0)