Java Program to Find the Occurrence of Words in a String using HashMap Last Updated : 19 Dec, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report HashMap<Key, Value> provides the basic implementation of the Map interface of Java and import java.util.HashMap package or its superclass. HashMap stores the data in (Key, Value) pairs, and accessed by an index of another type (e.g. an Integer). One object is used as a key to another object. If the duplicate key is inserted, it will replace the element of the corresponding key. Approach : Declare a HashMap in Java of <String, Integer>Split the given string and store the words into a String array.Traversing the array, check if the word is in the HashMap or not.If it is not in the HashMap, then store the word as key and 1 as the initial value; if the word is present in the HashMap then increase the value against the word.Once the traversal is complete, print the HashMap.Example: Java // Java Program to find the occurrence // of words in a String using HashMap. import java.io.*; import java.util.HashMap; import java.util.Map; class GFG { public static void main(String[] args) { // Declaring the String String str = "Alice is girl and Bob is boy"; // Declaring a HashMap of <String, Integer> Map<String, Integer> hashMap = new HashMap<>(); // Splitting the words of string // and storing them in the array. String[] words = str.split(" "); for (String word : words) { // Asking whether the HashMap contains the // key or not. Will return null if not. Integer integer = hashMap.get(word); if (integer == null) // Storing the word as key and its // occurrence as value in the HashMap. hashMap.put(word, 1); else { // Incrementing the value if the word // is already present in the HashMap. hashMap.put(word, integer + 1); } } System.out.println(hashMap); } } Output{Bob=1, Alice=1, and=1, is=2, girl=1, boy=1}Note that in the above code, we have used Wrapper class i.e. Integer, since the get(key) method returns null. You can also eliminate the use of integer variable from the above program. Let us see the code below : Java // Java Program to find the occurrence // of words in a String using HashMap. import java.io.*; import java.util.HashMap; import java.util.Map; class GFG { public static void main(String[] args) { String str = "Alice is girl and Bob is boy"; Map<String, Integer> hashMap = new HashMap<>(); String[] words = str.split(" "); for (String word : words) { // containsKey(key) will return a boolean value // i.e. true if it contains the key and false if // it doesn't. if (hashMap.containsKey(word)) hashMap.put(word, hashMap.get(word) + 1); else hashMap.put(word, 1); } System.out.println(hashMap); } } Output{Bob=1, Alice=1, and=1, is=2, girl=1, boy=1} Comment More infoAdvertise with us Next Article Java Program to Find the Occurrence of Words in a String using HashMap A ankitsinghrajput Follow Improve Article Tags : Java Java Programs Java-HashMap Java-String-Programs Practice Tags : Java Similar Reads Java Program to Find Occurrence of a Word Using Regex Java's regular expressions, or regex, let you do advanced text manipulation and matching. Regex offers a handy approach for searching for a term in a text wherever it appears. In this article, we will learn to find every occurrence of a word using regex. Program to Find Occurrence of a Word Using Re 2 min read Java program to count the occurrence of each character in a string using Hashmap Given a string, the task is to write a program in Java which prints the number of occurrences of each character in a string. Examples: Input: str = "GeeksForGeeks" Output: r 1 s 2 e 4 F 1 G 2 k 2 o 1 Input: str = "Ajit" Output: A 1 t 1 i 1 j 1 An approach using frequency[] array has already been dis 2 min read Java Program to Search a Particular Word in a String Using Regex In Java string manipulation, searching for specific words is a fundamental task. Regular expressions (regex) offer a powerful and flexible approach to achieve this search. It matches the patterns simply by comparing substrings. In this article, we will learn how to search for a particular word in a 3 min read Java Program to Print all Unique Words of a String Java program to print all unique words present in the string. The task is to print all words occurring only once in the string. Illustration: Input : Welcome to Geeks for Geeks. Output : Welcome to for Input : Java is great.Python is also great. Output : Java Python also Methods: This can be done in 4 min read Java Program to Check Strings Anagram Using HashMap Java Program to check whether two given strings are anagrams of each other or not using HashMap. What is an Anagram?A string is an anagram of another string if it contains the same characters in the same or in different order. Example of Anagram in JavaInput: s1 = "abcd" s2 = "cadb"Output: Two Strin 3 min read Like