Getting the count of duplicate values in arrayList in Java without affecting the order of arrayList
本问题已经有最佳答案,请猛点这里访问。
我试图得到
这是我的
1 2 3 4 5 6 7 8 9 10 11 12 13 | Map<String, Integer> counts = new HashMap<String, Integer>(); for (String str : t.courseName) { if (counts.containsKey(str)) { counts.put(str, counts.get(str) + 1); } else { counts.put(str, 1); } } for (Map.Entry<String, Integer> entry : counts.entrySet()) { System.out.println(entry.getKey() +" =" + entry.getValue()); } |
获取
使用
A LinkedHashMap is a combination of hash table and linked list. It has a predictable iteration order (a la linked list), yet the retrieval speed is that of a HashMap. The order of the iteration is determined by the insertion order, so you will get the key/values back in the order that they were added to this Map.