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]);
}
}
}
标签:map,String,treeKey,treeMap,mapValue,key,put,排序 来源: https://www.cnblogs.com/rxqdsg/p/14824455.html