Sort characters in a string from large to small by number of characters

Source: Internet
Author: User

Recently, a colleague went out to interview, there is a about the string array of strings sorted by the number of occurrences, from large to small, required to write within 5 minutes. It was awkward, not written. I'll take a look back, too.

For this:

1 String stri = "AGASDFASDFDCCVVASDFG";

or this

1 String str = "A,B,C,A,V,D,F,S,S,F,F,SD";

In fact, they are all the same.

The first implementation of:

Idea: 1. Convert it to an array, 2. Defines a value that is map,key as a character/string, and value is the number of occurrences

3. Sort the map's value collection, which is sorted from large to small

4. Traverse the sorted value collection, if the original map contains this value, the key and value will be re-written to the Linkedhashmap

Code implementation:

1  Public classTestSortDemo1 {2      Public Static voidMain (string[] args) {3String str = "AGADFSFFDFVASDF";4         Final Char[] chars =Str.tochararray ();5map<character,integer> map =NewHashmap<>();6          for(Charachar:chars) {7             if(Map.containskey (Achar)) {8Map.put (Achar,map.get (Achar) +1);9}Else {TenMap.put (achar,1); One             } A         } - System.out.println (map); -list<integer> list =NewArraylist<>(); the          for(Integer integer:map.values ()) { - list.add (integer); -         } -         //Sort Values by +Collections.sort (list);//Positive Order -         //Collections.sort (List,comparator.reverseorder ());//Reverse + System.out.println (list); ALinkedhashmap<character,integer> Linkedhashmap =NewLinkedhashmap<> ();//does not change the order of insertions at          for(Integer value:list) { -              for(Character character:map.keySet ()) { -                 if(Map.get (character) = =value) { - Linkedhashmap.put (character,value); -                 } -             } in         } - System.out.println (linkedhashmap); to System.out.println (Linkedhashmap.keyset ()); +     } -}

Results of the operation:

To switch to the positive sequence run result:

Another way to achieve: After the third step slightly different, but the idea is basically the same, 3. Convert map to EntrySet, package into List 4, call Collections.sort method, override comparison method

Specific code implementation:

1  Public Static voidMain (string[] args) {2String str = "AGADFSFFDFVASDF";3         Final Char[] chars =Str.tochararray ();4Map<character, integer> map =NewHashmap<>();5          for(Charachar:chars) {6             if(Map.containskey (Achar)) {7Map.put (Achar, Map.get (Achar) + 1);8}Else {9Map.put (Achar, 1);Ten             } One         } A System.out.println (map); -list<map.entry<character,integer>> list =NewArraylist<>(); - List.addall (Map.entryset ()); theCollections.sort (list,NewComparator<map.entry<character, integer>>() { - @Override -              Public intCompare (Map.entry<character, integer> O1, Map.entry<character, integer>O2) { -                 returnO2.getvalue ()-o1.getvalue ();//from large to small
return O1.getvalue ()- o2.getvalue ();//Small to large + } - }); +System.out.println ("Order from large to small:"); A for(Map.entry<character, integer>characterintegerentry:list) { atSystem.out.print (Characterintegerentry.getkey () + ","); - } -}

Results of Transport:

Summarize:

The two ways of realizing the core ideas are the same, the main research on the mastery of the collection.

Something: A long time did not see, the fierce contact is still quite ignorant force. The record is a review of knowledge.

Sort characters in a string from large to small by number of characters

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.