統計一段文本中每個字符出現的次數 統計結果到Map集合, key是字符, value 是字符的個數

public class Test03 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Map<Character, Integer> map = new HashMap<Character, Integer>();
		String str = "abaacbbc";
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);		
			if(map.containsKey(c)){
				Integer value =map.get(c);
		           map.put( c,value+1 );
		       }else{
		    	   map.put(c, 1);
		       }

		    }System.out.println(map);
	}

}

發佈了111 篇原創文章 · 獲贊 41 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章