Map set important exercises, map set
Important exercise: Display letters in a string in the following format:
A (1) B (2 )......
The code and ideas are as follows:
/* Obtain the number of letters in the string and print the following format: a (1) B (2) c (3 )...... idea: first define a method to encapsulate this function. 1. first, convert the string into a character Array Using toCharArray (); 2. define a map container to receive each character, because the result is ordered, you can use TreeMap3. traverse the character array for loop. use a letter as the key to query the letter in the map set. If null is returned, it indicates that the letter does not exist in the map set. Then, the letter and 1 are saved to the map set, if the returned value is not null, it indicates that the letter is already in the map set. In the map set, the number of times of the letter is increased by 1, then, the letter and the number of auto-incrementing times are stored in the map set. after being saved, the original number of times will be overwritten. 3. Print the data in the map set in the specified format: 1. define a container. the StringBuilder buffer can store any data. You can use the KeySet method of the Map set or the entrySet method to obtain the map set. Get the value in the map set using the get (key) method of the keySet or the getKey () method of the entrySet method and the getValue () method to obtain the corresponding key and value. 2. finally, add the key and value to the buffer using the append method of StringBuilder and print it. because StringBuilder and String are not the same class, you must write toString ---> return sb when returning sb. toString (); */import java. util. *; class MapCharTest {public static void main (String [] args) {String s = myCharCount ("aabbccddedsf"); sop (s);} public static String myCharCount (String str) {// Converts a string to a character array char [] chs = str. toCharArray (); // defines a map set TreeMap <Character, Integer> tm = new TreeMap <Character, Integer> (); // traverses the Character array for (int x = 0; x <chs. length; x ++) {// use the letter as the key to find the corresponding Integer value = tm. get (chs [x]); // remove other characters not letters. if (! (Chs [x]> = 'A' & chs [x] <= 'Z' | chs [x]> = 'A' & chs [x] <= 'Z ')) continue; // determine whether the value exists if (value = null) tm. put (chs [x], 1); else {value = value + 1; tm. put (chs [x], value) ;}// sop (tm); // create a buffer zone StringBuilder sb = new StringBuilder (); // * // keySet method Set <Character> keySet = tm for the first Map fetch method. keySet (); iterates the key set Iterator <Character> it = keySet. iterator (); while (it. hasNext () {Character key = it. next (); // obtain the key Integer value = tm. get (key); // obtain the corresponding sb value through the key. append (key + "(" + value + ")"); // Add the key and value to the buffer} * // second Retrieve Method of Map set: entrySet Set <Map. entry <Character, Integer> entrySet = tm. entrySet (); Iterator <Map. entry <Character, Integer> it = entrySet. iterator (); while (it. hasNext () {Map. entry <Character, Integer> me = it. next (); Character key = me. getKey (); Integer value = me. getValue (); sb. append (key + "(" + value + ")");} return sb. toString (); // print StringBuilder in String format .} public static void sop (Object obj) {System. out. println (obj );}}