What is the best/easiest way to sort a large list of words (10,000-20,000) by the number of times they occur in the list, in Java. I tried a basic implementation but I get an ou
public List countOccurences(ArrayList list){
HashMap hm = new HashMap();
for (String s:list) {
Integer i = hm.get(s);
if (i == null){
i = 0;
}
i++;
hm.put(s, i);
}
List mapKeys = new ArrayList(hm.keySet());
List mapValues = new ArrayList(hm.values());
HashMap sortedMap = new LinkedHashMap();
TreeSet sortedSet = new TreeSet(mapValues);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
for (int i=0; i(sorted.keyset());
}