Java 获取重复的值 并统计总数

public static void main(String[] args) {
List l =new ArrayList();
l.add("a") ;
l.add("a") ;
l.add("b") ;
l.add("b") ;
l.add("b") ;
l.add("c") ;
l.add("d") ;
l.add("d") ;
l.add("e") ;
l.add("e") ;
l.add("e") ;
l.add("e") ;
l.add("e") ;
l.add("e") ;

    System.out.println(l);
    Map<String, Integer> map = new HashMap<String, Integer>();
    for(String item: l){
        if(map.containsKey(item)){
            map.put(item, map.get(item).intValue() + 1);
        }else{
            map.put(item, new Integer(1));
        }
    }
    Iterator<String> keys = map.keySet().iterator();
    while(keys.hasNext()){
        String key = keys.next();
        System.out.print(key + ":" + map.get(key).intValue() + ", ");
    }

}
原文地址:https://www.cnblogs.com/LQ970811/p/14309154.html