forked from sumeet-automation/selenium-java-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion26.java
More file actions
37 lines (27 loc) · 1.13 KB
/
Copy pathQuestion26.java
File metadata and controls
37 lines (27 loc) · 1.13 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
package string;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.*;
public class Question26 {
public static void main(String[] args) {
String word = "HelloWorld";
System.out.println(wordFrequencyLambda(word));
System.out.println(wordFrequencyCollectGroupBy(word));
}
private static Map<String, Integer> wordFrequencyLambda(String word) {
Map<String, Integer> wordMap = new HashMap<>();
Stream.of(word.split("")).forEach(letter->
wordMap.compute(letter, (key, value) -> wordMap.get(key) == null ? 1 : value + 1));
return wordMap;
}
private static Map<String, Long> wordFrequencyCollectGroupBy(String word) {
return Stream.of(word.split("")).collect(groupingBy(identity(), counting()));
}
/*private static Map<String, Integer> wordFrequencyCollectToMap(String word) {
return Stream.of(word.split(""))
.collect(toMap(identity(),1,(value1,value2)->(value1)));
String mutiLine = "hellohow";
}*/
}