Hashmap sorting programming instance-selected from csdn post

Source: Internet
Author: User
/*
In HashMap, how to sort by value first and then by key
Example: Map <String, Integer> result = new HashMap <String, Integer> ();
Sort by numbers and then by letters. Place the key-value in HashMap to TreeMap.
And then sort the rows? It is best to give an example.

*/
/*
TreeMap does not work. TreeMap can only be sorted by key!

Definition of the constructor with comparator in the TreeMap api doc:

TreeMap (Comparator <? Super K> c)

Obviously, the comparator accepts the comparison of keys rather than values.
*/
Import java. util. ArrayList;
Import java. util. Collections;
Import java. util. Comparator;
Import java. util. HashMap;
Import java. util. Iterator;
Import java. util. LinkedHashMap;
Import java. util. List;
Import java. util. Map;

Public class Test {

Public static void main (String [] args ){
Map <String, Integer> map = new HashMap <String, Integer> ();
Map. put ("ee", 3 );
Map. put ("B", 1 );
Map. put ("d", 2 );
Map. put ("eee", 3 );
Map. put ("A", 1 );
Map. put ("K", 2 );
Map. put ("ade", 1 );
Map. put ("c", 2 );
Map. put ("aee", 3 );
Map. put ("a", 1 );
Map. put ("faed", 2 );
Map. put ("bdd", 1 );
Map. put ("qec", 2 );
Map. put ("eade", 3 );
Map. put ("Aadf", 1 );
Map. put ("Kqe", 2 );

Map <String, Integer> sortMap = new Test (). sortMap (map );

For (Map. Entry <String, Integer> entry: sortMap. entrySet ()){
System. Out. println (entry. getkey () + "-->" + entry. getvalue ());
}
}

Public <K, V extends number> Map <string, V> sortmap (Map <string, V> map ){
Class mymap <m, n> {
Private M key;
Private N value;
Private M getkey (){
Return key;
}
Private void setkey (M key ){
This. Key = key;
}
Private n getvalue (){
Return value;
}
Private void setvalue (N value ){
This. value = value;
}
}

List <mymap <string, V> List = new arraylist <mymap <string, V> ();
For (Iterator <String> I = map. keySet (). iterator (); I. hasNext ();){
MyMap <String, V> my = new MyMap <String, V> ();
String key = I. next ();
My. setKey (key );
My. setValue (map. get (key ));
List. add (my );
}

Collections. sort (list, new Comparator <MyMap <String, V> (){
Public int compare (MyMap <String, V> o1, MyMap <String, V> o2 ){
If (o1.getValue () = o2.getValue ()){
Return o1.getKey (). compareTo (o2.getKey ());
} Else {
Return (int) (o1.getValue (). doubleValue ()-o2.getValue (). doubleValue ());
}
}
});

Map <string, V> sortmap = new linkedhashmap <string, V> ();
For (INT I = 0, K = List. Size (); I <K; I ++ ){
Mymap <string, V> my = list. Get (I );
Sortmap. Put (My. getkey (), My. getvalue ());
}


/*
Return (INT) (o1.getvalue (). doublevalue ()-o2.getvalue (). doublevalue ());

Returns the comparison results of values between two objects. For example, if one is 5 and the other is 3, their difference is 2, that is, 2> 0,
Therefore, 5 is behind 3. If it is less than 0, it will be placed in the front. If it is equal to 0, we will compare the size of the key.

Such sorting rules should be what you want.

The code I posted on the 12th floor has some problems. Correct it:

   Collections. Sort (list, new comparator <mymap <string, V> (){
Public int compare (MyMap <String, V> o1, MyMap <String, V> o2 ){
If (o1.getValue (). equals (o2.getValue () {// change this line
Return o1.getKey (). compareTo (o2.getKey ());
} Else {
Return (int) (o1.getValue (). doubleValue ()-o2.getValue (). doubleValue ());
}
}
});
*/
Return sortmap;
}
}

Selected from: http://topic.csdn.net/u/20080531/17/158772eb-e31d-4152-afea-09731c473e12.html

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.