zoukankan      html  css  js  c++  java
  • Hashtable类中的四种遍历方法对比

    要遍历一个Hashtable,api中提供了如下几个方法可供我们遍历:

     

      keys() - returns an Enumeration of the keys of this Hashtable

      keySet() - returns a Set of the keys

      entrySet() - returns a Set of the mappings

      elements() - returns an Enumeration of the values of this Hashtable


    4种方法,那种更好呢,写段代码来比较一下吧:

    import java.util.Enumeration;
    import java.util.Hashtable;
    import java.util.Iterator;
    import java.util.Map.Entry;

    public class HashtableTest {
         public static void main(String[] args) {
               long start = 0;
               long end = 0;
              
              Hashtable<String, String> table = new Hashtable<String, String>();
               forint i = 0; i < 1000000; i++) {
                  table.put( "key:" + i, "value:" + i);
              }
              
               //1、使用keys()
              start = System. currentTimeMillis();
              Enumeration<String> en1 = table.keys();
               while(en1.hasMoreElements()) {
                  en1.nextElement();
              }
              end = System. currentTimeMillis();
              System. out.println( "Enumeration keys costs " + (end - start) + " milliseconds" );
              
               //2、使用elements()
              start = System. currentTimeMillis();
              Enumeration<String> en2 = table.elements();
               while(en2.hasMoreElements()) {
                  en2.nextElement();
              }
              end = System. currentTimeMillis();
              System. out.println( "Enumeration elements costs " + (end - start) + " milliseconds" );
              
               //3、使用keySet()
              start = System. currentTimeMillis();
              Iterator<String> it1 = table.keySet().iterator();
               while(it1.hasNext()) {
                  it1.next();
              }
              end = System. currentTimeMillis();
              System. out.println( "Iterator keySet costs " + (end - start) + " milliseconds" );
              
               //4、使用entrySet()
              start = System. currentTimeMillis();
              Iterator<Entry<String, String>> it2 = table.entrySet().iterator();
               while(it2.hasNext()) {
                  it2.next();
              }
              end = System. currentTimeMillis();
              System. out.println( "Iterator entrySet costs " + (end - start) + " milliseconds" );
         }
    }

    运行结果如下:
    Enumeration keys costs 16 milliseconds
    Enumeration elements costs 15 milliseconds
    Iterator keySet costs 17 milliseconds
    Iterator entrySet costs 16 milliseconds

    通过运行结果显示,在如今jdk的版本下,两者差距不是很大,不过使用Enumeration去遍历会稍微快一些。
  • 相关阅读:
    P5664 Emiya 家今天的饭
    P3944 肮脏的牧师
    P1233 木棍加工
    P4017 最大食物链计数
    P1287 盒子与球
    Java之未来已来(1)
    java-信息安全(二)-对称加密算法DES,3DES,AES,Blowfish,RC2,RC4
    java-信息安全(一)-BASE64,MD5,SHA,HMAC,RIPEMD算法
    SpringBoot集成Caffeine作本地缓存
    联想拯救者-触摸板手势
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/5631851.html
Copyright © 2011-2022 走看看