HashMap sort by value from big to small, value equals key from small to large

Source: Internet
Author: User
Tags comparable

HashMap is sorted by value from large to small, and value is equal to the key from small to large.

[2] is from small to large sort, in the [2] code based on the exchange of O1,o2 position can be.

The code uses the method mentioned in [1] to compare key when value is equal.

    Staticmap Sortbyvalue (map map) {List List=NewLinkedList (Map.entryset ()); Collections.sort (list,NewComparator () { Public intCompare (Object O1, Object O2) {if((( comparable) ((Map.entry) (O2). GetValue ()). CompareTo ((((map.entry) (O1 )). GetValue (())? =n) {                     return( (comparable) ((Map.entry) (O2). GetValue ()). CompareTo (((map.entry) (O1)). GetValue ()); }Else {                      return( (comparable) ((Map.entry) (O1). GetKey ()). CompareTo (((Map.entry) (O2)). GetKey ()); }             }        });

[1]key,value Simultaneous sorting http://stackoverflow.com/a/3074324/1767800

[2] Sort by value http://stackoverflow.com/a/109389/1767800

HashMap sort by value from big to small, value equals key from small to large

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.