Sort a HashMap to make it an ordered Map.
How to sort values in HashMap? Many people have encountered this problem. A common solution is to use LinkedHashMap, because LinkedHashMap can remember the order in which elements are put, it can be thought of as a true "ordered" (it is impossible to make HashMap orderly). I prefer it. However, the problem is that data is often encapsulated in HashMap, and we must manually sort the data before putting it into HashMap. This is of course the idea, and the code implementation is also very simple, it looks quite comfortable to write, just remember:
Map<String,String> myMapTmp = XXDao().getXXX(); Map<String,String> myMap = new LinkedHashMap<String,String>(); List<String> keyList = new ArrayList<String>(); Iterator<String> it =myMapTmp.keySet().iterator(); while(it.hasNext()){ keyList.add(it.next()); } Collections.sort(keyList); Iterator<String> it2 = groupidList.iterator(); while(it2.hasNext()){ String key = it2.next(); attrOfBisGroups.put(key, myMap.get(key)); }
It is worth noting that the Collections. sort () method is used to sort the specified list in ascending order based on the natural order of elements. All elements in the list must implement the Comparable interface. In addition, all elements in the list must be comparable to each other (that is, for any e1 and e2 elements in the list, e1.compareTo (e2) cannot throw ClassCastException ). I have tested that the String type can directly use this interface. If the elements in your list are custom, you must implement Comparable and compile the comparator yourself.
After talking about this, the text is actually here. The results of queryForMap with ibatis are unordered, even if there is orderby in SQL, even if the ibatis file specifies the return value type (ordered type, such as TreeMap and LinkedHashMap), I do not know how ibatis fills data, so we can only sort in the memory, but it is also a way, at the same time, to see if there is any other way to tell.
Java Map sorting number
I suggest using TreeMap because it is ordered. If you must use LinkedHashMap, you can convert it to TreeMap for execution.
Import java. util. Iterator;
Import java. util. Set;
Import java. util. TreeMap;
Public class Test1 {
/**
* @ Param args
*/
Public static void main (String [] args ){
TreeMap <Integer, String> maps = new TreeMap <Integer, String> ();
Maps. put (3, "Wang Wu ");
Maps. put (1, "Zhang San ");
Maps. put (2, "Li Si ");
Maps. put (4, "Zhao Liu ");
Int size = maps. size ();
Int count = 0;
Set set = maps. keySet ();
Iterator iter = set. iterator ();
While (iter. hasNext ()){
Object key = iter. next ();
If (count> = (size-2 )){
System. out. println (key + "" + maps. get (key ));
}
Count ++;
}
}
}
How can Hashmap be sorted during output?
Hashmap () is unordered.
If it is in order, we recommend that you use LinkedHashMap. TreeMap. LinkedHashMap is output in the input order, while TreeMap is output in the Key order. If your "george, henry, billy, harry" is the value, use the two above. sort and output the data. If hashmap is used, it will not work.