Java "AABABCABCDABCDE" to get the number of occurrences of each letter in a string

Source: Internet
Author: User

Requirement: "AABABCABCDABCDE" to get the number of occurrences of each letter in a string


Analysis:
* A: Define a string (can be improved for keyboard entry)
* B: Define a TreeMap Collection
* Key: Character
* Value: Integer
* C: Convert string to character array
* D: Iterate through a character array to get each character
* E: Take the character you just got as a key to the set to find the value, see the return value
* is null: Indicates that the key does not exist, the character is used as the key, and 1 is stored as a value
* NOT NULL: Indicates that the key exists, add a value of 1, and then override to store the key and value
* F: Defines a string buffer variable
* G: Iterate through the set, get the keys and values, make the stitching as required
* H: Converts a string buffer to a string output


public class MapTest1 {public static void main (string[] args) {//define a string (can be improved for keyboard entry) string content = "AABABCABCDABCDE";// Define a TreeMap collection Treemap<character, integer> TM = new Treemap<character, integer> ();//Convert a string to a character array char[] CHS = Content.tochararray ();//Iterate through the character array, get each character for (char Ch:chs) {///take the character you just obtained as a key into the set to find the value, see the return value integer i =  tm.get (ch);// Is null: Indicates that the key does not exist, the character is used as the key, and 1 is stored as the value if (i = = null) {tm.put (ch, 1);} else {//is NOT NULL: Indicates that the key exists, adds a value of 1, and then overrides store the key and value I++;tm.put (Ch,i);}} Define the string buffer variable StringBuilder sb=  new StringBuilder ();//Iterate through the collection, get the keys and values, make the stitching set<character> set = Tm.keyset () as required; for (Character key:set) {Integer value = Tm.get (key); Sb.append (Key). Append ("("). Append (Value). Append (")");} Converts a string buffer to a string output of the strings result = Sb.tostring (); SYSTEM.OUT.PRINTLN ("Result:" +result);}}


Java "AABABCABCDABCDE" to get the number of occurrences of each letter in a string

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.