Java map by value sort

Source: Internet
Author: User
Import Java. io. file; import Java. util. arraylist; import Java. util. collections; import Java. util. comparator; import Java. util. hashmap; import Java. util. list; import Java. util. map; import Java. util. map. entry; public class statics {/*** stores the search keyword and the corresponding search times */private Map <string, integer> keywordmap = new hashmap <string, integer> (); /*** add search keyword ** @ Param keyword */Public void addkeyword (string keyword) {If (keywordmap. containskey (keyword) {int num = keywordmap. get (keyword); num ++; keywordmap. put (keyword, num);} else {keywordmap. put (keyword, 1) ;}/ *** sort by value, from large to small ** @ Param keywordmap * hashmap * @ return */public list of search keywords <entry <string, integer> sortkeywordmap (Map <string, integer> keywordmap) {list <entry <string, integer> arraylist = new arraylist <entry <string, integer> (keywordmap. entryset (); collections. sort (arraylist, new comparator <entry <string, integer> () {public int compare (Entry <string, integer> E1, entry <string, integer> E2) {return (e2.getvalue ()). compareto (e1.getvalue () ;}}); For (Entry <string, integer> entry: arraylist) {system. out. println (entry. getkey () + "" + entry. getvalue ();} return arraylist;}/*** sorts the hashmap of the Search Keyword by value, from large to small ** @ return */public list <entry <string, integer> sortkeywordmap () {return sortkeywordmap (keywordmap );}

 

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.