【面试题】给出一串字符,找出每个字符出现的次数

package com.test;

import java.util.HashMap;
import java.util.Set;

/**
 * Created by hejin on 2017/7/25 0025.
 */
public class CountStringNum {

    public static void main(String[] args) {
        String codes="TT,HH,EE,YY,UU,OO,TT,HH,OO,QQ,SS,KK,OO,QQ,TT,ZZ";
        countStringNum(codes);

    }

    public static void countStringNum(String codes){
        String[] codeList = codes.split(",");
        HashMap<String, Integer> map = new HashMap();
        for (String s:codeList){
            if(map.containsKey(s)){
                map.put(s,map.get(s)+1);
            }else{
                map.put(s,1);
            }

        }
        Set<String> strings = map.keySet();
        for (String s: strings){
            System.out.println("字符"+s+"出现的次数为:"+map.get(s));
        }


    }
}

 

posted @ 2017-07-25 16:20  Againn  阅读(396)  评论(0编辑  收藏  举报