Method for statistics of strings per character in Java _java

Source: Internet
Author: User

Copy Code code as follows:

/*
String name = "ADSBSADGSADGTEWTERFSDF";
Eg a-->6,b-->1 d-->3 ...
The string is stored in the map collection frame with a (letter) =>2 (number)
Train of thought: 1. Converts a string into a character array.
2. Define a map collection, and then iterate over the character array, if the element is not in the map collection, the element is stored in the map set, and a counter is defined, and the number is stored in the map to achieve the goal

*/
Import java.util.*;
Class Charmapdemo
{
public static void Main (string[] args)
{
String str = "ABCEDSADGSADFASDFAA";
treemap<character,integer> TM = Pross (str);
SYSTEM.OUT.PRINTLN (tm);
}
public static treemap<character,integer> Pross (String str)
{
char[] Chararray = Str.tochararray ();

treemap<character,integer> TM = new treemap<character,integer> ();

for (int x = 0; x < Chararray.length + +)
{
if (!tm.containskey (Chararray[x]))
{
Tm.put (chararray[x],1);
}
Else
{
int count = Tm.get (chararray[x]) +1;
Tm.put (Chararray[x],count);
}
}
return TM;
}
}

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.