I wrote a code that counts every character in a given string and displays it. Now I want to modify my code, so it shows first character, that is mostly repeated, then second, third.
For example, "Java".
Result should be : a=2, j=1, v=1.
Map<Character,Integer> occurrences = new HashMap<>();
char[] chars = str2.toCharArray();
for(char character : chars) {
Integer integer = occurrences .get(character);
occurrences .put(character, integer);
if (integer == null) {
occurrences .put(character, 1);
} else {
occurrences .put(character, integer + 1);
}
}
System.out.println(occurrences );
question from:
https://stackoverflow.com/questions/65661145/how-to-sort-a-character-by-number-of-occurrences-in-a-string-using-map-collectio 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…