String s = "abcosjifinvaaijfdnvpkcmsnoianindqnccmcxdnivnwifnc";
Map<Character, Integer> map = new HashMap();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
//System.out.println(c);
if (map.get(c) != null) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
ArrayList<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
for (Object msg : list) {
System.out.print(msg + " ");
}
Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
@Override
public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
return o2.getValue()-o1.getValue();
}
});
System.out.println();
for (Object msg : list) {
System.out.print(msg + " ");
}
Collections用法:https://blog.csdn.net/weixin_38336658/article/details/87869695