Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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);
}
}
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #word #count #string #HashMap #Collection
ADD COMMENT
Topic
Name
2+4 =