实现原理--
利用map中的key/value,key 表示字符,value表示出现的次数
String content ="i like like like java do you like a java";
byte[] contentBytes = content.getBytes();
System.out.println(contentBytes.length);
Map<Byte,Integer> map = getNodes(contentBytes);
//打印map
for(Map.Entry<Byte, Integer> entry:map.entrySet()){
System.out.println("字母:"+(char)(entry.getKey()+0)+" 次数:"+entry.getValue());
}
public static Map getNodes(byte[] bytes){
//遍历bytes,统计每一个byte出现的次数-->map[key,value]
Map<Byte,Integer> counts = new HashMap<>();
for(byte b : bytes){
Integer count = counts.get(b);
if(count==null){
counts.put(b,1);
}else{
counts.put(b, count+1);
}
}
}
实现截图:
image.png