Monday, October 20, 2014

Count of repetable words from a string

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class test {
public static void main(String[] args)
{
String s = "i an Apples is an an Apple";

String ar[] = s.split(" ");
List<String> valuesList = Arrays.asList(ar);

Map<String, Integer> unique = new HashMap<String, Integer>();

for (String string : valuesList) {
if(unique.get(string) == null)
unique.put(string, 1);
else
unique.put(string, unique.get(string) + 1);
}
System.out.println("Values = " + unique);

}

No comments:

Post a Comment