使用HashMap计算每个Key出现的次数

 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         }

猜你喜欢

转载自www.cnblogs.com/jijunjie/p/9921349.html