如何遍历HashTable

package   com.zcjl.test;  
   
  import   java.util.Hashtable;  
  import   java.util.Iterator;  
   
  public   class   TestHashtable   {  
          public   static   void   main(String[]   args)   {  
                  Hashtable   table   =   new   Hashtable();  
                  table.put("1",   "AAA");  
                  table.put("2",   "BBB");  
                  table.put("3",   "CCC");  
                  table.put("4",   "DDD");  
                  table.put("5",   "EEE");  
                  for   (Iterator   it2   =   table.keySet().iterator();   it2.hasNext();)   {  
                          String   key   =   (String)   it2.next();  
                          System.out.println(key   +   table.get(key));  
                  }  
          }  
  }  
   
  结果为  
  5EEE  
  4DDD  
  3CCC  
  2BBB  
  1AAA  
   
   
   
  package   com.zcjl.test;  
   
  import   java.util.HashMap;  
  import   java.util.Iterator;  
   
  public   class   TestHashMap   {  
          public   static   void   main(String[]   args)   {  
                  HashMap   hashMap   =   new   HashMap();  
                  hashMap.put("1",   "AAA");  
                  hashMap.put("2",   "BBB");  
                  hashMap.put("3",   "CCC");  
                  hashMap.put("4",   "DDD");  
                  hashMap.put("5",   "EEE");  
                  for   (Iterator   it   =   hashMap.keySet().iterator();   it.hasNext();)   {  
                          String   key   =   (String)   it.next();  
                          System.out.println(key   +   hashMap.get(key));  
                  }  
          }  
  }  
   
  结果为:  
  3CCC  
  5EEE  
  2BBB  
  4DDD  
  1AAA  
   
  从结果看,也许Hashtable能保证按加入的顺序遍历?  
  这个测试太简单,所以我不敢保证结论
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值