Requirement: "AABABCABCDABCDE" to get the number of occurrences of each letter in a string
Analysis:
* A: Define a string (can be improved for keyboard entry)
* B: Define a TreeMap Collection
* Key: Character
* Value: Integer
* C: Convert string to character array
* D: Iterate through a character array to get each character
* E: Take the character you just got as a key to the set to find the value, see the return value
* is null: Indicates that the key does not exist, the character is used as the key, and 1 is stored as a value
* NOT NULL: Indicates that the key exists, add a value of 1, and then override to store the key and value
* F: Defines a string buffer variable
* G: Iterate through the set, get the keys and values, make the stitching as required
* H: Converts a string buffer to a string output
public class MapTest1 {public static void main (string[] args) {//define a string (can be improved for keyboard entry) string content = "AABABCABCDABCDE";// Define a TreeMap collection Treemap<character, integer> TM = new Treemap<character, integer> ();//Convert a string to a character array char[] CHS = Content.tochararray ();//Iterate through the character array, get each character for (char Ch:chs) {///take the character you just obtained as a key into the set to find the value, see the return value integer i = tm.get (ch);// Is null: Indicates that the key does not exist, the character is used as the key, and 1 is stored as the value if (i = = null) {tm.put (ch, 1);} else {//is NOT NULL: Indicates that the key exists, adds a value of 1, and then overrides store the key and value I++;tm.put (Ch,i);}} Define the string buffer variable StringBuilder sb= new StringBuilder ();//Iterate through the collection, get the keys and values, make the stitching set<character> set = Tm.keyset () as required; for (Character key:set) {Integer value = Tm.get (key); Sb.append (Key). Append ("("). Append (Value). Append (")");} Converts a string buffer to a string output of the strings result = Sb.tostring (); SYSTEM.OUT.PRINTLN ("Result:" +result);}}
Java "AABABCABCDABCDE" to get the number of occurrences of each letter in a string