Map<Long, Integer> map = new HashMap<Long, Integer>(); // MYObject:爲自定義類,取id做爲key,而後,進行數目統計 for (MyObject temp : list) { Integer count = map.get(temp.getId()); map.put(temp.getId(), (count == null) ? 1 : count + 1); } // 打印統計 for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); }