mapValue排序


import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Test {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
map.put("A",1);
map.put("B",6);
map.put("C",5);
map.put("D",7);
map.put("E",5);
TreeMap<Integer,String> treeMap = new TreeMap();
for (String key:map.keySet()){
Integer treeKey = map.get(key);
if (treeMap.containsKey(treeKey)){
treeMap.put(treeKey,treeMap.get(treeKey)+","+key);
}else {
treeMap.put(treeKey,key);
}
}
String[]arry = new String[map.size()];
int end = map.size()-1;
for (Integer key1:treeMap.keySet()){
String []arry2 = treeMap.get(key1).split(",");
for (int i=0;i<arry2.length;i++){
arry[end]=arry2[i]+":"+key1;
end--;
}
}
for (int j=0;j<arry.length;j++){
System.out.println(arry[j]);
}




}
}
上一篇:java list集合


下一篇:HashMap和TreeMap类