可以的,你可以看下TreeMap API帮助文档的介绍
例子如下
@Test
public void test() {
TreeMap
treeMap.put(0.1003, "zhangsan");
treeMap.put(1.1, "lisi");
treeMap.put(1.00031, "wangwu");
treeMap.put(0.1102, "xiaohong");
Set
for(Iterator
Entry
System.err.println("KEY:"+entry2.getKey()+" "+"VALUE:"+entry2.getValue());
}
}
------------------------------------
运行结果:
KEY:0.1003 VALUE:zhangsan
KEY:0.1102 VALUE:xiaohong
KEY:1.00031 VALUE:wangwu
KEY:1.1 VALUE:lisi
按照大小排序了
import java.util.Comparator;
import java.util.TreeMap;
public class MapPaixu {
public static void main(String[] args) {
TreeMapmap2 = new TreeMap (new Comparator () {
@Override
public int compare(Double o1, Double o2) {
return o2.compareTo(o1);
}
});
map2.put(1.3, 2);
map2.put(1.2, 4);
map2.put(-2.02133, 3);
map2.put(1.1, 1);
map2.put(1.8, 2);
map2.put(-1.01, 3);
System.out.println("逆序排列:"+map2);
}
}
运行结果: