Frequency(Occurance) of words in String

Frequency of words in String , we can count(occurrence) number of words in a string
SOURCE : FrequecyWord.java


import java.util.HashMap;
import java.util.StringTokenizer;

public class FrequecyWord {

public static void main(String[] args) {


String currentLine = "india is a great country and its people are of india";
HashMap frequencyMap = new HashMap();



StringTokenizer st = new StringTokenizer(currentLine, " \t\n\r\f.,;:!?'\"");
while(st.hasMoreTokens()){
String currentWord = st.nextToken();

Integer freq = frequencyMap.get(currentWord);

if(freq==null){
freq=0;
}
frequencyMap.put(currentWord,freq+1);

}
System.out.println(frequencyMap);
}


}




INPUT

sample input :- "india is a great country and its people are of india";




OUTPUT


{a=1, country=1, are=1, and=1, of=1, its=1, is=1, india=2, great=1, people=1}







Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment