How to find Frequency Of words in a Given String using Array List

How to find Frequency Of words in a Given String using Array List.
SOURCE : UsingList.java

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

public class UsingList {

public static void main(String[] args) {

String s = "india is my country and india is only the way out india and and their";
s = s.trim();
List ls = Arrays.asList(s.split(" "));
HashSet hs = new HashSet(ls);
for (String word : hs) {

System.out.println(word + ":::" + Collections.frequency(ls, word));

}

}

}

INPUT

String s = "india is my country and india is only the way out india and and their"



OUTPUT

country:::1
their:::1
is:::2
india:::3
my:::1
way:::1
out:::1
the:::1
and:::3
only:::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