用Map求String数组中相同字符串的个数

public class Test {
    public static void main(String[] args) {
        //字符作为key 字符数作为value
        String[] strs={"a","f","s","s","s"};

        Map<String,Integer> map=new HashMap();
        for(String o : strs){
            if(map.containsKey(o)){
                int count=map.get(o)+1;
                map.put(o,count);

            }else{
                map.put(o,1);
            }

        }
        for(String o : map.keySet()){
            System.out.println(o+":"+map.get(o));
        }

    }
}

猜你喜欢

转载自blog.csdn.net/Ein_Blatt/article/details/86165462