The number of times the Map set app retrieves letters from a string. For example: String: & quot; abcdekka27qoq & quot; & #160;, output format: a (2) B (1) k (2)..., mapabcdekka27qoq

Source: Internet
Author: User

The number of times the Map set app retrieves letters from a string. For example: String: "abcdekka27qoq", output format: a (2) B (1) k (2)..., mapabcdekka27qoq

Package com. swift; import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. map. entry; import java. util. set; public class Test3_String_char {public static void main (String [] args) {/** 4th question: the number of times letters in a String appear. For example: String: "abcdekka27qoq", output format: a (2) B (1) k (2 )... */String str = "abcdekka27qoq"; char [] charArray = str. toCharArray (); Map <Character, Integer> map = new HashMap <Character, Integer> (); // the first time, the same elements are automatically filtered out and put into map (char c: charArray) {map. put (c, 0);} Set <Character> set = map. keySet (); for (char c: set) {System. out. print (c + "(" + map. get (c) + ")");} // get the original array for the second time. Compared with map, if it contains num + 1, delete the incorrect one and add the correct one, add 1 more, delete the delimiters, and add the correct for (char c: c HarArray) {if (map. containsKey (c) {int num = map. get (c) + 1; map. remove (c); map. put (c, num) ;}} System. out. println (); for (Map. entry <Character, Integer> entry: map. entrySet () {System. out. print (entry. getKey () + "(" + entry. getValue () + ")");} System. out. println (); Set <?> EntrySet = map. entrySet (); Iterator <?> It = entrySet. iterator (); while (it. hasNext () {Entry <Character, Integer> entry = (Entry <Character, Integer>) it. next (); System. out. print (entry. getKey () + "(" + entry. getValue () + ")");}}}

 

Related Article

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.