package com.zhiyou.entity;
import java.util.HashMap;
import java.util.Map;
public class ZYtongjicishu {
public static <K, V> void main(String[] args) {
// 统计这句话中每个字母出现的次数
//打印次数最多和最少的两个字母
String str ="The best way to cheer yourself up is to try to cheer somebody else up.";
Map<String,Integer> maps = new HashMap<String,Integer>();
for(int i=0;i<str.length();i++){
String key = String.valueOf((str.charAt(i)));
if(!maps.containsKey(key))
maps.put(key, 1);
else{
int val =maps.get(key);
maps.put(key, val+1);
}
}
for(Map.Entry i : maps.entrySet()){
System.out.println(i.getKey()+ "=="+i.getValue());
}
int max = 0; // 记录最大出现次数
int min=0;
int[] cnt = new int[150]; // 临时计数用的数组
for (int i = 0; i < str.length(); i++) { // 循环字符以做统计
char c = str.charAt(i); // 取出单个字母
max = (++cnt[c] > max) ? cnt[c] : max; // 计数并检测最大出现次数
}
System.out.println ("出现次数最多的次数:"+max);
}
}