Find a way to repeat the number of letters in an English alphabet (Java)

Source: Internet
Author: User
Tags repetition

The first thing you need to know about Java's HashMap data type:

HashMap is the implementation of a hash table-based map interface. HashMap has two elements, one is key (key name), one is value (key value), it is equivalent to a dictionary, and the dictionary in Python is the same.

Find out how the repetition of each letter is achieved in an English alphabet:

The letters appearing in the text, as the key name (key), the number of occurrences as the key value (value), the key name in the HashMap can not be duplicated, then the number of these letters is counted as the number of these same key names.

The implementation can be from the beginning of the first letter, the letter into the hash table, the first is a:1, and then in order to save the 2nd letter f:1, if there is a repetition with the previous letter, the key value of the preceding letter + 1, such as AFA, traversing to the 3rd letter, a key value becomes 2, that a : 2, if you encounter space numbers, punctuation, and other special characters are excluded, do not insert, if you encounter punctuation marks also statistics do not need to judge the conditions, all inserted can.

The code is as follows:

1 ImportJava.util.HashMap;2 
3 4 Public classHashmap_examp {5 6 Public Static voidMain (string[] args) {7hashmap<string,integer> map =NewHashmap<string,integer>();8String str= "afashfjgjkhlnmash,^566* integer GSHKJSHGKSDFSGDFHJK";9 for(intI=0;i<str.length (); i++){Ten CharC=Str.charat (i); OneString key=string.valueof (c); A if(Map.containskey (key)) { -Integer value=Map.get (key); -Map.put (Key, value+1); the } - Else{ - //map.put (key, 1); Statistics of all characters, including Chinese - if(c>= ' A ' &&c<= ' Z ' | | C>= ' a ' &&c<= ' Z ') {//use ASCII code to remove strings of numbers, spaces, punctuation, special characters +Map.put (Key, 1); - } + } A } at System.out.println (map); - } -}

Output Result:

{f=4, g=4, d=2, s=6, A=3, N=1, l=1, M=1, j=4, k=4, h=6}

Find a way to repeat the number of letters in an English alphabet (Java)

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.