Importjava.util.*;
Public classMapsortdemo {
Public static voidMain (string[] args) {
map<string, string> map =NewHashmap<> ();
Map.put ("1","4");
Map.put ("3","3");
Map.put ("2","2");
Map.put ("4","1");
//map<string, string> resultmap = Sortmapbykey (MAP); Sort by key: nature of the 1.treemap
map<string, string> resultmap =SortMapByKey2(map);//Sort by key: 2.list, Custom Comparator (requires linkedhashmap after sorting)
map<string, string> resultmap = sortmapbyvalue (map); Sort by value: list, custom comparer
for(Map.entry<string, String> entry:resultMap.entrySet ()) {
System. out. println (Entry.getkey () +" " + Entry.getvalue ());
}
}
/**
* Sort by key using the properties of TreeMap
* @paramMap
* @return
*/
Public StaticMap<string, string> Sortmapbykey (map<string, string> Map) {
if(Map = =NULL|| Map.isempty ()) {
return null;
}
//treemap By default with the natural ordering of key, so do not declare the comparator can also implement key ordering, the comparator can customize the collation, such as reverse
map<string, string> sortmap = new treemap<string, string> ();
The TreeMap construction method can have a comparator parameter ~ But the comparator can only be compared to key
map<string, string> sortmap =NewTreemap<string, String> (NewMapkeycomparator ());
Sortmap.putall (map);
returnSortmap;
}
/**
* Use list to sort by key
* @paramMap
* @return
*/
Public StaticMap<string, string> SortMapByKey2 (map<string, string> Map) {
if(Map = =NULL|| Map.isempty ()) {
return null;
}
list<map.entry<string,string>> list =NewArraylist<> (Map.entryset ());
Collections.Sort(List,NewMapKeyComparator2 ());
map<string, string> sortmap =NewLinkedhashmap<> ();
iterator<map.entry<string, string>> iterable = List.iterator ();
while(Iterable.hasnext ()) {
map.entry<string, string> tmpentry = Iterable.next ();
Sortmap.put (Tmpentry.getkey (), Tmpentry.getvalue ());
}
returnSortmap;
}
/**
* Use list to sort map by value
* @paramOrimap
* @return
*/
Public StaticMap<string, string> sortmapbyvalue (map<string, string> orimap) {
if(Orimap = =NULL|| Orimap.isempty ()) {
return null;
}
//Must be linkedhashmap, because Linkedhashmap guarantees that the put order and output order are consistent!
map<string, string> sortedmap =NewLinkedhashmap<> ();
Map.entry the map <key,value> when the node is loaded into the list, sort the list
list<map.entry<string, string>> entrylist =NewArraylist<> (Orimap.entryset ());
Collections.Sort(Entrylist,NewMapvaluecomparator ());
Iterator<map.entry<string, string>> iter = Entrylist.iterator ();
map.entry<string, string> tmpentry =NULL;
while(Iter.hasnext ()) {
Tmpentry = Iter.next ();
Sortedmap.put (Tmpentry.getkey (), Tmpentry.getvalue ());
}
returnSortedMap;
}
}
classMapkeycomparatorImplementscomparator<string> {
@Override
public intCompare (String str1, String str2) {
returnStr2.compareto (STR1);
}
}
classMapvaluecomparatorImplementsComparator<map.entry<string, string>> {
@Override
public intCompare (map.entry<string, string> me1, map.entry<string, string> me2) {
returnMe1.getvalue (). CompareTo (Me2.getvalue ());
}
}
classMapKeyComparator2ImplementsComparator<map.entry<string, string>> {
@Override
public intCompare (map.entry<string, string> me1, map.entry<string, string> me2) {
returnMe1.getkey (). CompareTo (Me2.getkey ());
}
}
JAVA Map Comparison by Key,value