長い文字列があり、各文字の出現回数を統計し、逆記述に従って出力する必要がある.
7425 ワード
/**
* ClassName: CountStr
*
* @author leegoo
* @Description:
* @date 2019 07 03
*/
public class CountStr {
private static String str = "" +
"Hello i'am jack," +
"very glad to be here," +
"my favourite subject is English . " +
"and i like sports very much . ";
public static void main(String[] args) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c != 32) { //
if (map.containsKey(c)) {
Integer countNumber = map.get(c);
map.put(c, ++countNumber);
} else {
map.put(c, 0);
}
}
}
//map https://www.cnblogs.com/yucfeng/p/8794608.html
map.entrySet().stream().sorted(Map.Entry.<Character, Integer>comparingByValue().reversed()).forEach(
e -> System.out.println("key:"+e.getKey()+",value="+e.getValue())
);
}
}
結果は次のとおりです.
key:e,value=8
key:i,value=5
key:a,value=4
key:l,value=4
key:r,value=4
key:s,value=4
key:o,value=3
key:t,value=3
key:c,value=2
key:h,value=2
key:m,value=2
key:u,value=2
key:v,value=2
key:y,value=2
key:b,value=1
key:d,value=1
key:g,value=1
key:j,value=1
key:k,value=1
key:,,value=1
key:n,value=1
key:.,value=1
key:E,value=0
key:H,value=0
key:f,value=0
key:',value=0
key:p,value=0