Count the number of letters in the Map set, and count the letters in the map set

Source: Internet
Author: User

Count the number of letters in the Map set, and count the letters in the map set

Map set exercise:
"AsfefxAAcf34vrfdfse2-2asd -- wdd" gets the number of times each letter in the string appears
The printed result is: a (2) c (1 )...;
Ideas:
Result analysis shows that there are many mappings between letters and occurrences in the results,
A lot of data needs to be stored. Arrays and Map sets can store mappings.
Is there a fixed order in the relationship? No, so the Map set is selected.
It is also found that the parties that can be uniquely identified have a natural order, that is, the alphabetic order;
Therefore, TreeMap storage is selected.

The final storage in the collection is a ing relationship that uses letters as the key and the number of occurrences of letters as the value.
1. Because the operation is a character, the string is first converted into a character array.
2. traverse the character array and use the letter as the key to judge whether the element is included in the Map set,
If the key is not included, the key is used as the key and the value is 1, which is saved to the Map set;
If it contains, take out the value corresponding to the letter, add one, and store the letter and the new value into the set, so that the new value will overwrite the old value.
3. After the traversal is completed, the value corresponding to each letter is the number of times it appears in the string,
Print and output the set.

1 public class MapTest {2 3 public static void main (String [] args) {4 String str = "asfefxAAcf34vrfdfse2-2asd -- wdd"; 5 String s = countChar (str); 6 System. out. println (s); 7 8} 9 // defines the static method for calculating the number of occurrences of letters. 10 public static String countChar (String str) {11 char [] chs = str. toCharArray (); 12 Map <Character, Integer> tm = new TreeMap <Character, Integer> (); 13 for (int I = 0; I <chs. length; I ++) {14 // only count the number of occurrences of letters, not count other characters 1 5 if (! (Chs [I]> = 'A' & chs [I] <= 'Z' | chs [I]> = 'A' & chs [I] <= 'Z ')) 16 continue; 17 int count = 1; 18 Integer value = tm. get (chs [I]); 19 if (value! = Null) 20 count + = value; 21 tm. put (chs [I], count); 22 23} 24 return printMap (tm ); 25} 26 // define the method for printing sets by specifications 27 private static String printMap (Map <Character, Integer> tm) {28 StringBuilder sb = new StringBuilder (); 29 Iterator <Character> it = tm. keySet (). iterator (); 30 while (it. hasNext () 31 {32 Character key = it. next (); 33 Integer value = tm. get (key); 34 sb. append (key + "(" + value + ")"); 35} 36 return sb. toString (); 37} 38}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.