PackageTingjizifu;ImportJava.util.*; Public classTongji {/** Use scanner to read a string from the console, count the number of occurrences of each character in the string, and require that you use the learned knowledge to complete the above requirements * implementation ideas based on the set, List, map set characteristics. */ Public Static voidMain (string[] args) {//Input StringScanner input =NewScanner (system.in); String Shuru=Input.next (); //to load a string character into the list collectionlist<string> list =NewArraylist<string>(); for(inti = 0; I < shuru.length (); i++) {List.add (string.valueof (Shuru.charat (i))); } //to load a string character into a set setSet<string> set =NewHashset<string>(); for(inti = 0; I < shuru.length (); i++) {Set.add (string.valueof (Shuru.charat (i))); } //The characters in set are compared with the characters in the list, and the same is added 1 to count.//The character in the set set is then used as the key (key), the number of statistics as the value (value), and the data in the map is finally printed.map<string, integer> map =NewHashmap<string, integer>(); for(String str:set) {intsum = 0; //System.out.println (str); for(inti = 0; I < list.size (); i++) { if(List.get (i). Equals (str)) {sum++; }} map.put (str, sum); } Set<String> SS =Map.keyset (); for(String str:ss) {System.out.println (str+ "occurrences" + map.get (str) + "Times"); } input.close (); }}
About the use of list, set set, and map