日期:2014-05-20 浏览次数:20715 次
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Map<Integer,String> map = new TreeMap<Integer, String>(new
Comparator<Integer,String>()
{
public int compare( <Integer,String> a, <Integer,String> b)
{
if(a.getValue()compareTo(b.getValue()) > 0)
{
return 1;
}
else if(a.getValue().compareTo(b.getValue()) == 0)
{
return 0;
}
else
{
return -1;
}
}
});
map.put(20, "abc");
map.put(13, "ggg");
map.put(40, "qqq");
map.put(12, "yyy");
}
}
Map<Integer, String> map = new TreeMap<Integer, String>();
map.put(20, "abc");
map.put(13, "ggg");
map.put(40, "qqq");
map.put(12, "yyy");
List<Map.Entry<Integer, String>> list = new ArrayList<Map.Entry<Integer, String>>();
for (Map.Entry<Integer, String> entry : map.entrySet()) {
list.add(entry);
}
java.util.Collections.sort(list, new Comparator<Map.Entry<Integer, String>>() {
@Override
public int compare(Map.Entry<Integer, String> o1, Map.Entry<Integer, String> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
Map<Integer, String> rsMap = new LinkedHashMap<Integer, String>();
for (Map.Entry<Integer, String> entry : list) {
rsMap.put(entry.getKey(), entry.getValue());
}