I would also use HashMap. I found the code where I did this:
HashMap<String, Integer> counts = new HashMap<String, Integer>(); void increment(String s) { Integer oldCount = counts.get(s); if (oldCount == null) { counts.put(s, 1); } else { counts.put(s, oldCount + 1); } }
List of items:
Map.Entry<String, Integer>[] array = new Map.Entry[counts.size()]; counts.entrySet().toArray(array); Arrays.sort(array, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) { return b.getValue() - a.getValue(); } }); int x = 0, min = 0; for (Map.Entry<String, Integer> el : array) { String k = el.getKey(); println("Count: " + el.getValue() + "\n" + k + "\n\n"); }
source share