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

public static void main(String[] args) {
    List<String> l =new ArrayList<String>();
    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() + ", ");
    }

}
posted @ 2021-01-21 17:20  葵儿啊  阅读(622)  评论(0编辑  收藏  举报
/*粒子线条,鼠标移动会以鼠标为中心吸附的特效*/