forked from sumeet-automation/selenium-java-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion30.java
More file actions
69 lines (58 loc) · 2.32 KB
/
Copy pathQuestion30.java
File metadata and controls
69 lines (58 loc) · 2.32 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package string;
import java.util.*;
import java.util.stream.Stream;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.*;
public class Question30 {
public static void main(String[] args) {
System.out.println(wordFrequency("AAABBCDA"));
System.out.println(wordFrequency_Compute("AAABBCDA"));
System.out.println("wordFrequency_Merge: "+wordFrequency_Merge("AAABBCDA"));
System.out.println(wordFrequency_Lambdas("AAABBCDA"));
List<Integer> list = List.of(1, 2, 1, 23, 34, 23, 1);
Set<Integer> set = new HashSet<>(list);
System.out.println(set);
}
private static Map<String, Integer> wordFrequency_Merge(String word) {
Map<String, Integer> map = new LinkedHashMap<>();
Arrays.stream(word.split("")).forEach(key -> map.merge(key, 1, (k1, v1) -> v1 + 1));
return map;
}
private static Map<Character, Integer> wordFrequency(String word) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < word.length(); i++) {
char key = word.charAt(i);
if (map.containsKey(key))
map.put(key, (map.get(key) + 1));
else
map.put(key, 1);
}
return map;
}
private static Map<String, Integer> wordFrequency_Split(String word) {
Map<String, Integer> map = new HashMap<>();
for (String key:word.split("")) {
if (map.containsKey(key))
map.put(key, (map.get(key) + 1));
else
map.put(key, 1);
}
return map;
}
private static Map<Character, Integer> wordFrequency_Compute(String word) {
Map<Character, Integer> map = new TreeMap<>();
for (int i = 0; i < word.length(); i++) {
char key = word.charAt(i);
map.compute(key, (k1, v1) -> map.get(k1) == null ? 1 : v1 + 1);
}
return map;
}
private static Map<String, Integer> wordFrequency_Lambdas(String word) {
return Stream.of(word.split(""))
.collect(groupingBy(identity(), summingInt(val -> 1)));
}
private static Map<String, Long> wordFrequency_LambdasCounting(String word) {
return Stream.of(word.split(""))
.collect(groupingBy(identity(), counting()));
}
}