package jp.co.jid.collectionSample; import java.util.TreeMap; public class StringCount { public static void main(String[] args) { System.out.println("-- 演習⑪ --"); // キーボードから文字を入力 String inputStr = InputUtil.input(); // 文字列を1文字ずつに分割 String[] inputStrList = inputStr.split(""); // 分割した文字を登録しながらカウントアップ TreeMap<String, Integer> map = new TreeMap<String, Integer>(); for (String str : inputStrList) { if (!map.containsKey(str)) { map.put(str, Integer.valueOf(0)); } map.put(str, Integer.valueOf(map.get(str).intValue() + 1)); } // 表示処理 for (String str : map.keySet()) { System.out.println(str + ":" + map.get(str).intValue() + "回"); } } }