I wrote code that counts frequency of every character in a given string and displays it:
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);
Now I want to modify my code, so it shows the characters ordered by their frequency. Starting with the character, that is most frequently repeated, then second most frequently, then third and so on.
For example the string Java
should be displayed as character-frequency in following order: a=2, j=1, v=1
.