diff --git a/DuplicateWordCount.java b/DuplicateWordCount.java new file mode 100644 index 0000000..3060166 --- /dev/null +++ b/DuplicateWordCount.java @@ -0,0 +1,19 @@ +public class DuplicateWordCount { + + public static void main(String[] args) { + String str = "hello hi there hello"; + String[] strArr = str.split(" "); + + HashMap hashMap = new HashMap<>(); + for (String s : strArr) { + Integer count = hashMap.get(s); + hashMap.put(s, count == null ? 1 : count + 1); + } + + Map hashMapCountGreaterThanTwo = hashMap.entrySet().stream() + .filter(entry -> entry.getValue() > 1) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + + System.out.println(hashMapCountGreaterThanTwo); + } +}