在HashMap中,怎样先按value排序,再按key排序
例如:Map<String,Integer> result = new HashMap<String,Integer>();
先按数字排序,再按字母排序。把HashMap中的key-value 放到TreeMap
中再排序行吗?最好给个例子。
例如:Map<String,Integer> result = new HashMap<String,Integer>();
先按数字排序,再按字母排序。把HashMap中的key-value 放到TreeMap
中再排序行吗?最好给个例子。
中,再对所得的TreeMap排序行吗?
然后用Collections.sort(list<T>);
排序完成后,重新写回 Map 中,写回时采用 LinkedHashMap 可以保证迭代的顺序。下面的代码可以参考一下:import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;public class Test { public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("ee", 3);
map.put("b", 1);
map.put("d", 2);
map.put("eee", 3);
map.put("A", 1);
map.put("K", 2);
map.put("ade", 1);
map.put("c", 2);
map.put("aee", 3);
map.put("a", 1);
map.put("faed", 2);
map.put("bdd", 1);
map.put("qec", 2);
map.put("eade", 3);
map.put("Aadf", 1);
map.put("Kqe", 2); Map<String, Integer> sortMap = new Test().sortMap(map); for(Map.Entry<String, Integer> entry : sortMap.entrySet()) {
System.out.println(entry.getKey() + " --> " + entry.getValue());
}
} public <K, V extends Number> Map<String, V> sortMap(Map<String, V> map) {
class MyMap<M, N> {
private M key;
private N value;
private M getKey() {
return key;
}
private void setKey(M key) {
this.key = key;
}
private N getValue() {
return value;
}
private void setValue(N value) {
this.value = value;
}
} List<MyMap<String, V>> list = new ArrayList<MyMap<String, V>>();
for (Iterator<String> i = map.keySet().iterator(); i.hasNext(); ) {
MyMap<String, V> my = new MyMap<String, V>();
String key = i.next();
my.setKey(key);
my.setValue(map.get(key));
list.add(my);
} Collections.sort(list, new Comparator<MyMap<String, V>>() {
public int compare(MyMap<String, V> o1, MyMap<String, V> o2) {
if(o1.getValue() == o2.getValue()) {
return o1.getKey().compareTo(o2.getKey());
}else{
return (int)(o1.getValue().doubleValue() - o2.getValue().doubleValue());
}
}
}); Map<String, V> sortMap = new LinkedHashMap<String, V>();
for(int i = 0, k = list.size(); i < k; i++) {
MyMap<String, V> my = list.get(i);
sortMap.put(my.getKey(), my.getValue());
}
return sortMap;
}
}
不行,TreeMap 是按照 key 来进行排序的,不能按照 value 来排序。
return (int)(o1.getValue().doubleValue() - o2.getValue().doubleValue())
吗
因此 5 就排在了 3 的后面。如果是小于 0 的,就会排在前面,等于 0 时,我们就去比较 key 的大小。这样的排序规则应该是你想要的,呵呵。我在 12 楼贴的代码有点问题,改正一下: Collections.sort(list, new Comparator<MyMap<String, V>>() {
public int compare(MyMap<String, V> o1, MyMap<String, V> o2) {
if(o1.getValue().equals(o2.getValue())) { // 改成这一行
return o1.getKey().compareTo(o2.getKey());
}else{
return (int)(o1.getValue().doubleValue() - o2.getValue().doubleValue());
}
}
});
TreeMap 不行,TreeMap 只能按照 key 来排序!TreeMap 的 API DOC 中带有比较器的构造方法定义:TreeMap(Comparator<? super K> c)很明显,比较器接受的是 key 的比较,而不是值。
为什么要doubleValue()
return (o1.getValue()-o2.getValue())为什么不行??
还有火龙果你在12 楼贴的代码可以得到我要得结果,你可以试试。
MAP不是一个映射而已嘛....不了解
Map <String,Integer> result = new HashMap <String,Integer>();//待排序的HashMap
Map <String,String> sort = new TreeMap<String,String>();//创建一个TreeMap,TreeMap本身的Key集合就是排序的。
for(String key:result.keySet()){//迭代HashMap中的元素,构成TreeMap中的元素。
sort.put(result.get(key).toString()+key, key);//将HashMap中的value值和key值拼接成字符串,构成TreeMap的Key
}
//最后,sort中key的先后顺序就是楼主要求的先后顺序,key对应的value值是HashMap中的Key值。
一个装key-value,另一个装value-key,分别排序
import java.util.TreeSet;
import java.util.Set;
import java.util.Collection;
public class HashMapSort{
public static void main(String [] args){
HashMap<String,Integer> hm = new HashMap<String,Integer>();
hm.put("a",5);
hm.put("c",7);
hm.put("d",8);
hm.put("v",0);
hm.put("m",3);
hm.put("u",10);
hm.put("e",2);
hm.put("g",6); Set<String> set = hm.keySet();//取出所有键
TreeSet<String> ts =new TreeSet<String>();
System.out.println("键的原序:");
for(String s: set){
System.out.println(s);
ts.add(s);
}
System.out.println("按键排序后的顺序:");
for(String s:ts){
System.out.println(s);
}
Collection<Integer> cl= hm.values();//取出所有值
TreeSet<Integer> ts1 =new TreeSet<Integer>();
System.out.println("值的原序:");
for(Integer s: cl){
System.out.println(s);
ts1.add(s);
}
System.out.println("按值排序后的顺序:");
for(Integer s:ts1){
System.out.println(s);
}
}
}小试了一下,不知符不符合要求!试试吧,应该差不多。只要稍改一下就完全可以了。我想是这样。
import java.util.TreeSet;
import java.util.Set;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
public class HashMapSort{
public static void main(String [] args){
Map <String,Integer> hm= new LinkedHashMap<String,Integer>(); hm.put("a",5);
hm.put("c",7);
hm.put("d",8);
hm.put("v",0);
hm.put("m",3);
hm.put("u",10);
hm.put("e",2);
hm.put("g",6);
System.out.println("键的顺序:");
for(String s: set){System.out.println(s);}
}
}