-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathDuplicateWordCount.java
More file actions
28 lines (23 loc) · 881 Bytes
/
DuplicateWordCount.java
File metadata and controls
28 lines (23 loc) · 881 Bytes
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
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Input- hello there hello hi there hello
* Output- {there=2, hello=3}
*/
public class DuplicateWordCount {
public static void main(String[] args) {
String str = "hello there hello hi there hello";
String[] strArr = str.split(" ");
HashMap<String, Integer> hashMap = new HashMap<>();
for (String s : strArr) {
Integer count = hashMap.get(s);
hashMap.put(s, count == null ? 1 : count + 1);
}
System.out.println(hashMap);
Map<String, Integer> hashMapCountGreaterThanTwo = hashMap.entrySet().stream()
.filter(entry -> entry.getValue() > 1)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.println(hashMapCountGreaterThanTwo);
}
}