import java.util.*; public class TestStrStatistics { public static void main(String[] args) { String[] arr = { "a", "b", "d", "d", "a", "d", "a", "e", "d", "c" }; HashMap<String,Integer> hashmap = strStatistics(arr); ArrayList<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(hashmap.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return o2.getValue()-o1.getValue(); } }); for(Map.Entry<String,Integer> listson:list){ System.out.println(listson.getKey()+":"+listson.getValue()); } } public static HashMap<String,Integer> strStatistics(String[] arr){ HashMap<String,Integer> hashMap = new HashMap<String,Integer>(); for(String str:arr){ if(hashMap.containsKey(str)){ int valueFrequency = hashMap.get(str); hashMap.put(str,++valueFrequency); }else { hashMap.put(str,1); } } return hashMap; } }