Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

word count in a string using HashMap Collection

import java.util.HashMap;

public class WordCount {
public static void main(String[] args) {

String str = "Hello World, Welcome to Softhunt";
String[] split = str.split(" ");

HashMap<String, Integer> map = new HashMap<String, Integer>();

for (int i = 0; i < split.length; i++) {
if (map.containsKey(split[i])) {
int count = map.get(split[i]);
map.put(split[i], count + 1);
} else {
map.put(split[i], 1);
}
}
System.out.println(map);
}
}
Comment

PREVIOUS NEXT
Code Example
Java :: what is a do while loop in java 
Java :: java search arraylist 
Java :: convert list of dto to map java 
Java :: get arguments in fragment kotlin 
Java :: add a value to a list java in java hashmap 
Java :: override class java 
Java :: what is var in java 
Java :: stringbuilder insert beginning 
Java :: Create class from string variable JAVA 
Java :: prime number program in java 
Java :: java string from byte array 
Java :: java how to fill an array 
Java :: iterate list in java 
Java :: java string character at index 
Java :: jpa page sort 
Java :: java boucle for 
Java :: hide app after install android studio 
Java :: how to concatenate two strings in java 
Java :: activityViewModels 
Java :: how to use map, filter and reduce in Java 
Java :: java sort method 
Java :: how to disable screenshot in react native 
Java :: cant change button color when app run android studio 
Java :: java calendar 
Java :: how to find the length of an array in java 
Java :: crit chance in java 
Java :: How to get the nth Fibonacci number code in Java using recursion with memoization 
Java :: could not find java in a java_home at /opt/java/openjdk/bin/java docker sonarqube 
Java :: interface java 
Java :: square star pattern in java 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =