This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
/
AdvancedExample.java
51 lines (42 loc) · 1.74 KB
/
AdvancedExample.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
package org.enumerable.lambda.enumerable;
import org.enumerable.lambda.annotation.LambdaParameter;
import org.enumerable.lambda.enumerable.collection.EList;
import org.enumerable.lambda.weaving.LambdaLoader;
import org.enumerable.lambda.weaving.Version;
import java.util.Map;
import static org.enumerable.lambda.Lambda.λ;
import static org.enumerable.lambda.Parameters.s;
import static org.enumerable.lambda.Parameters.t;
import static org.enumerable.lambda.enumerable.collection.ECollections.list;
import static org.enumerable.lambda.enumerable.collection.ECollections.pair;
public class AdvancedExample {
public static void main(String[] args) {
LambdaLoader.bootstrapMainIfNotEnabledAndExitUponItsReturn(args);
System.out.println("[example] " + Version.getVersionString());
new AdvancedExample().run();
}
@LambdaParameter
static Map.Entry<String, EList<String>> e;
@LambdaParameter
static Map.Entry<String, Integer> f;
private void run() {
String text = "a b c a c d e f a a b b";
System.out.println(
list(text.split(" "))
.groupBy(λ(s, s)) //unfortunately this reads nicer than identity() as the latter one does need explicit type argument
.collect(λ(e, pair(e.getKey(), e.getValue().size()))) // perhaps i would like to have collectValues() method to make this more readable
.sortBy(λ(f, f.getKey()))
.collect(λ(f, f.getKey() + " => " + f.getValue()))
.inject(λ(s, t, s + "\n" + t))
);
/*
Will output something along these lines:
a => 4
b => 3
c => 2
d => 1
e => 1
f => 1
*/
}
}