count the occurence/duplicate of word in the given string - Java
import java.util.HashMap;
import java.util.Map;
public class test3 {
public static void main(String[] args) {
String s1 = "hare krishn hare krishn krishn krishn hare hare. hare ram hare ram ram ram hare hare";
String s2 = s1.replace(".","");
String arr[]=s2.split(" ");
Map<String,Integer> hm = new HashMap<String,Integer>();
for(String val: arr){
if(hm.containsKey(val)){
hm.put(val,hm.get(val)+1);
}else{
hm.put(val,1);
}
}
hm.forEach((a,b)-> {if(a.equals("ram")) System.out.println(a+""+b);}); //for specific key
// to print complete map -> System.out.println(hm);
}
}
Comments
Post a Comment