public class TestMap {
public static void main(String[] args) {
HashMap<Integer,String> maps=new HashMap<Integer,String>();
maps.put(1,"Tom" );
maps.put(2,"LiLy" );
maps.put(12,"Lucy");
maps.put(10,"Tom");
maps.put(6,"Jim" );
maps.put(5,"Jim");
Set<Map.Entry<Integer,String>> entrys=maps.entrySet();
TreeMap<Integer,String> treeMap= new TreeMap<Integer,String>(maps);
Set<Map.Entry<Integer,String>> treeEntrys=treeMap.entrySet();
for(Map.Entry<Integer,String> entry:entrys)
{
System.out.println(entry.getKey()+":"+entry.getValue());
}
我这个程序运行的结果是:
1:Tom
2:LiLy
5:Jim
6:Jim
10:Tom
12:Lucy
我感觉很奇怪,是谁给排的序呢,我打算用TreeMap来对键值集进行排序呢,那个给排好了,就不用TreeMap来做了。那TreeMap的自然排序是什么呢? |
|