Sort the map collection by value from large to small

Source: Internet
Author: User

Overview:

Basic Features:

The collection stores key-value pairs and guarantees the uniqueness of the keys

Sub-class:

The |--hashtable is a hash data table structure and cannot use NULL as a key or value; The collection thread is synchronous

The |--hashmap is a hash data table structure that can use NULL as a key or value, and the collection thread is not synchronized

|--treemap Bottom is a two-fork tree structure, the thread is not synchronized, you can sort the key values in the map

Two ways to take out a map collection (principle: Convert the Map collection to set, and then use an iterator)

1. Incoming Map Collection

Public static map Sortbycomparator (map Unsortmap) {
List List = new LinkedList (Unsortmap.entryset ());
//System.out.println ("list:" +list);
Collections.sort (list, new Comparator ()
{
public int Compare (object O1, Object O2)
{
return ((comparable) ((Map.entry) (O2)). GetValue ())
. CompareTo (((map.entry) (O1)). GetValue ());
}
});
Map sortedmap = new Linkedhashmap ();

For (Iterator it = List.iterator (); It.hasnext ();) {
map.entry Entry = (map.entry) it.next ();
Sortedmap.put (Entry.getkey (), Entry.getvalue ());
}
return sortedmap;

}

Sort the map collection by value from large to small

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.