To solve this problem in Java, we can use a HashMap to store the count of each unique character in the input string. We will iterate through the string character by character and for each character, check if it exists in the HashMap. If it exists, we will increment the count by 1. If it doesn't exist, we will add the character to the HashMap with a count of 1.After iterating through the entire string, we need to sort the HashMap by keys in ascending order. We can convert the HashMap into a TreeMap and use the default sorting order of TreeMap that sorts keys in ascending order. Finally, we can print the count of each character in alphabetical order.Here is the Java code for the function:
```javaimport java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class CharacterCount {
public static void getCharacterCount(String input) {
Map charCount = new HashMap<>();
for (int i = 0;
i < input.length();
i++) {
char c = input.charAt(i);
if (charCount.containsKey(c)) {
charCount.put(c, charCount.get(c) + 1);
}
else {
charCount.put(c, 1);
}
}
Map sortedCharCount = new TreeMap<>(charCount);
for (char c :
sortedCharCount.keySet()) {
System.out.println(c + ":
" + sortedCharCount.get(c));
}
}
}
```To call this function, we can simply pass the input string as a parameter:
```javaCharacterCount.getCharacterCount("hello world");
```This will output:
```d:
1e:
1h:
1l:
3o:
2r:
1w:
1
```In this output, each line represents a unique character in the input string followed by its count.You can further improve this function by handling edge cases such as null or empty strings. You can also use regular expressions to remove non-alphabetic characters from the input string if required.Sources:- [Oracle Java Documentation - HashMap](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html)- [Oracle Java Documentation - TreeMap](https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html)