Claim:
Calculating a number of occurrences of each character string.
Experience: As used herein HashMap <K, V> advantage is that there will hashMap will automatically remove duplicate key
1. Obtain a string object
2. Create a Map collection of key representative character, the value represents the number of times.
3. traversal strings to get each character.
4 determines whether the key Map.
5. If not, the first time, stores a number of times; if so, description has occurred, the corresponding values acquired ++ stored again.
6. Print the final results
Code:
1 import java.util.HashMap; 2 import java.util.Scanner; 3 4 public class MapTest { 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 String str = input.nextLine(); 8 HashMap<Character, Integer> hashMap = new HashMap<>(); 9 for(int i = 0;i < str.length();i ++){ 10 char c = str.charAt(i); 11 IF (! hashMap.containsKey (C)) { 12 is hashMap.put (C,. 1 ); 13 is } 14 the else { 15 // public V GET (Object Key), acquires a value corresponding to the set in Map with a specified key. 16 Integer COUNT = HashMap.get (C); . 17 hashMap.put (C, COUNT ++); // not be used here ++ COUNT 18 is . 19 } 20 is } 21 is System.out.println (hashMap '); 22 is } 23 is }