forked from jbloch/effective-java-3e-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreq.java
More file actions
21 lines (16 loc) · 663 Bytes
/
Copy pathFreq.java
File metadata and controls
21 lines (16 loc) · 663 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package effectivejava.chapter7.item43;
import java.util.Map;
import java.util.TreeMap;
// Frequency table implemented with map.merge, using lambda and method reference (Page 197)
public class Freq {
public static void main(String[] args) {
Map<String, Integer> frequencyTable = new TreeMap<>();
for (String s : args)
frequencyTable.merge(s, 1, (count, incr) -> count + incr); // Lambda
System.out.println(frequencyTable);
frequencyTable.clear();
for (String s : args)
frequencyTable.merge(s, 1, Integer::sum); // Method reference
System.out.println(frequencyTable);
}
}