TreeMap Sort by key
The TreeMap object is required for the map to be sorted by key, because it is output by default in ascending order, it can be sorted by the comparer CompareTo, comparator can be used to sort the collection object or array, and the public of the interface is implemented. Compare (T O1,to2) method can be sorted, the method is mainly based on the first parameter O1, less than, equal to or greater than O2 to return a negative integer, 0 or a positive integer, if in ascending order can directly omit the comparator code map<string, string> TreeMap = new treemap<string, string> ();
The following code
map<string, string> treeMap = new treemap<string, string> (new comparator<string> () {@Overridepublic int compare (string O1, String O2) {//return O1.compareto (O2);//default: Return O2.compareto (O1) in ascending order; The descending order//return 0;//only returns the value of the first key stored, here is "CCCCC"}), Treemap.put ("C", "CCCCC"), Treemap.put ("A", "AAAAA"), Treemap.put (" B "," bbbbb "); Treemap.put (" D "," ddddd "); for (String Key:treeMap.keySet ()) {System.out.println (key+": "+treemap.get ( key));}
Output:
D:ddddd
C:ccccc
b:bbbbb
A:aaaaa
Sort value by value sort we need to use collections's sort (list<t> List, comparator<? Super T> C) method, The method sorts the specified list according to the order produced by the specified comparer. But there is a precondition that all elements must be able to be compared according to the comparator provided.
This method is generic to the key and value ordering, simply change the O1.getvalue (). CompareTo () in the Comparator (O2.getvalue ()) to Getkey ().
As follows:
map<string, string> treeMap = new treemap<string, string> (); Treemap.put ("C", "Jock"); Treemap.put ("A", " Rapido "), Treemap.put (" D "," free "), Treemap.put (" B "," Linda ");//Convert Map.entryset () to listlist<map.entry< String, string>> list = new arraylist<map.entry<string, string>> (Treemap.entryset ());// Then the comparator is implemented to sort Collections.sort (list, new comparator<map.entry<string, string>> () {@Overridepublic int Compare (entry<string, string> O1, entry<string, string> O2) {//return 0; Descending//Return O2.getvalue (). CompareTo (O1.getvalue ()); Descending return O1.getvalue (). CompareTo (O2.getvalue ()); ascending}); for (map.entry<string, string> mapping:list) {System.out.println (Mapping.getkey () + ":" + mapping.getval UE ());}
Output:
D:free
C:jock
B:linda
A:rapido
Attention:
After testing the sample code above,
Using the first anonymous inner class method to sort the key, if the comparator returns 0, the value is only the first output that is inserted, and only one piece of data is output.
Using the second list and comparer method to sort key or value, if the comparator returns 0, the value is the same as the reverse value, how much output. (If a friend knows why, please leave a message under the article, thank you)
HashMap sort
We are all HashMap values are not sequential, he is based on key hashcode to achieve. How are we going to achieve this sort of unordered hashmap? Referring to the value of TreeMap, we can also implement HashMap sort.
Sort by key
As with TreeMap, the new treemap<string, string> () will be changed to new hashmap<string, string> (), as in the case of the value comparison, Then replace the comparator's getvalue with a getkey on the line.
Sort by value
As with TreeMap, the new treemap<string, string> (), and the new hashmap<string, string> () will be the same as the value comparison method.
Other map types can be sorted by referring to the above methods
The Java Map collection utilizes the comparator comparator to sort by key and value