1 List<String> str = new ArrayList<>(); 2 str.add("a"); 3 str.add("a"); 4 str.add("b"); 5 6 Map<String,Object> map = new HashMap(); 7 8 for(String obj: str){ 9 if(map.containsKey(obj)){//判斷是否已經有該數值,如有,則將次數加1 10 map.put(obj, ((Integer)map.get(obj)).intValue() + 1); 11 }else{ 12 map.put(obj, 1); 13 } 14 }