Skip to content

Instantly share code, notes, and snippets.

@thanoojgithub
Last active November 28, 2015 19:53
Show Gist options
  • Save thanoojgithub/4182e4fc708eac7b3356 to your computer and use it in GitHub Desktop.
Save thanoojgithub/4182e4fc708eac7b3356 to your computer and use it in GitHub Desktop.
Word Count
package com.corejava
import java.util.Map;
import java.util.HashMap;
public class WordCount
{
public static void main (String[] args)
{
String str = "Today is a Holiday Day Today is a Working Day";
String[] wordArray = str.split("\\s+");
Map<String,Integer> map = new HashMap<String,Integer>();
for(String w : wordArray){
map.put(w, map.get(w) == null ? 1 : map.get(w)+1);
}
for(Map.Entry<String,Integer> entry : map.entrySet()){
System.out.println(entry.getKey() + " | " + entry.getValue());
}
}
}
@thanoojgithub
Copy link
Author

sample Word Count in java

@thanoojgithub
Copy link
Author

fixed compilation issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment