From 0b7fe9c5816ccceb39656d6ae771463596d52a66 Mon Sep 17 00:00:00 2001 From: HarshitSachdeva Date: Sat, 21 Oct 2023 01:34:52 +0530 Subject: [PATCH] Added prgram java --- DuplicateWordCount.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 DuplicateWordCount.java diff --git a/DuplicateWordCount.java b/DuplicateWordCount.java new file mode 100644 index 0000000..cc65eba --- /dev/null +++ b/DuplicateWordCount.java @@ -0,0 +1,24 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +public class DuplicateWordCount { + + public static void main(String[] args) { + String str = "hello 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); + } + System.out.println(hashMap); + + Map hashMapCountGreaterThanTwo = hashMap.entrySet().stream() + .filter(entry -> entry.getValue() > 1) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + + System.out.println(hashMapCountGreaterThanTwo); + } +}