I want to print the count of each word in an ArrayList in alphabetical order. I have implemented the code but for an element "hihi" in the list, I want the count should be 2 instead of 1. How to achieve that?
{//some code
Collections.sort(list);
System.out.println("Words with the count");
Map<String, Long> st1=new TreeMap<>();
for(String k : list){
st1.put(k,st1.getOrDefault(k, 0L)+1);
}
for(String k : st1.keySet()){
System.out.println(k+": "+st1.get(k));
}
//end of class}