public static void main(String[] args) {
countChar();
}
public static void countChar(){
Scanner scan = new Scanner(System.in);
String str = "";
System.out.println("请输入一行字符串:");
str = scan.next();
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < str.length(); i++) {
Character ch = new Character(str.charAt(i));
Integer val = new Integer(1);
Integer oldVal = map.put(ch, val);
if (oldVal != null){
map.put(ch, oldVal + 1);
}
}
System.out.println(map);
int max = 0;
Character ch1 = null;
Set<Character> set = new HashSet<Character>();
set = map.keySet();
Iterator<Character> it = set.iterator();
while (it.hasNext()){
Character ch = (Character)it.next();
Integer pos = map.get(ch).intValue();
if (pos > max){
max = pos;
ch1 = ch;
}
}
System.out.println("字符串" + str + "中出现最多的字符是:" + ch1 + "\n" + "出现次数为:" + max);
}