HashTable遍历效率比较

一、什么是Hashtable

Hashtable 类代表了一系列基于的哈希代码组织起来的/对。它使用键来访问集合中的元素。

二、4种遍历方法

HashTable 遍历的时候,有4种方法

  • Method1: 通过foreach遍历哈希表中的
  • Method2: 通过foreach遍历哈希表中的
  • Method3: DictionaryEntry 遍历哈希表中的键值
  • Method4:IDictionaryEnumerator 遍历哈希表中的键值

三、测试

生成1千万条hashtable数据进行比对:

static void Main(string[] args)
 {
 	//--------产生1000W条HashTable记录-------------------
     Hashtable hashtable = new Hashtable();
     for (int i = 0; i < 10000000; i++)
     {
         hashtable.Add(i.ToString(), "qcj");
     }
     Console.WriteLine("Ready? Let's Go!");
     Console.WriteLine(" ");
     Stopwatch watch = new Stopwatch();
     
     //遍历方法一:遍历哈希表中的键--------------------
     watch.Start();
     foreach (string key in hashtable.Keys)
     {
         //Console.WriteLine(string.Format("{0}-{1}"), key, hashtable[key]);
     }
     watch.Stop();
     Console.WriteLine("foreach遍历键:" + watch.Elapsed.TotalSeconds);
     Console.WriteLine("**********************************************************");
     
     //遍历方法二:遍历哈希表中的值----------------------
     watch.Restart();
     foreach (string value in hashtable.Values)
     {
        // Console.WriteLine(value);
     }
     watch.Stop();
     Console.WriteLine("foreach遍历值:" + watch.Elapsed.TotalSeconds);
     Console.WriteLine("**********************************************************");
     //遍历方法三:遍历哈希表中的键值-------------------
     watch.Restart();
     foreach (DictionaryEntry de in hashtable)
     {
        // Console.WriteLine(string.Format("{0}-{1}", de.Key, de.Value));
     }
     watch.Stop();
     Console.WriteLine("DictionaryEntry 遍历键值:" + watch.Elapsed.TotalSeconds);
     Console.WriteLine("**********************************************************");
     //遍历方法四:遍历哈希表中的键值------------------------
     watch.Restart();
     IDictionaryEnumerator myEnumerator = hashtable.GetEnumerator();
     bool flag = myEnumerator.MoveNext();
     while (flag)
     {
         //Console.WriteLine(myEnumerator.Key + "-" + myEnumerator.Value);
         flag = myEnumerator.MoveNext();
     }
     watch.Stop();
     Console.WriteLine("IDictionaryEnumerator 遍历键值:" + watch.Elapsed.TotalSeconds);
     Console.Read();
 }

最后结果如下:
在这里插入图片描述

四、结果

结果表明:

  • foreach遍历值和IDictionaryEnumerator遍历键值速度差不多

  • IDictionaryEnumerator要更快一点,而且能同时获得key 和 value

  • foreach遍历值的时候访问不到键,遍历键的时候访问不到值。

好的,我可以为你编写一个示例程序,分别演示如何向 `HashMap` 和 `Hashtable` 中插入 `Person` 对象,并且使用 `id` 作为键进行存储和查找。同时,我们可以比较它们的速度。 首先,我们需要定义一个 `Person` 类: ```java public class Person { private int id; private String name; private int age; public Person(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } } ``` 接下来,我们可以创建一个 `HashMap` 和一个 `Hashtable` 对象,并向它们中插入一些 `Person` 对象: ```java public static void main(String[] args) { Map<Integer, Person> hashMap = new HashMap<>(); Map<Integer, Person> hashTable = new Hashtable<>(); // 向 HashMap 中插入元素 long startTime1 = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { Person person = new Person(i, "Person" + i, i % 100); hashMap.put(person.getId(), person); } long endTime1 = System.currentTimeMillis(); // 向 Hashtable 中插入元素 long startTime2 = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { Person person = new Person(i, "Person" + i, i % 100); hashTable.put(person.getId(), person); } long endTime2 = System.currentTimeMillis(); System.out.println("HashMap 插入耗时:" + (endTime1 - startTime1) + "ms"); System.out.println("Hashtable 插入耗时:" + (endTime2 - startTime2) + "ms"); } ``` 这里,我们使用了一个循环来插入 100000 个 `Person` 对象,并使用 `id` 作为键进行存储。 最后,我们比较了向 `HashMap` 和 `Hashtable` 中插入元素的时间。这里,我们使用了 `System.currentTimeMillis()` 方法来测量时间。 运行程序,我们可以得到类似如下的输出: ``` HashMap 插入耗时:38ms Hashtable 插入耗时:53ms ``` 可以看到,向 `HashMap` 中插入元素的时间要比向 `Hashtable` 中插入元素的时间少。这是因为 `HashMap` 是线程不安全的,而 `Hashtable` 是线程安全的,因此在插入元素时,`Hashtable` 需要进行同步操作,会降低其效率。 当然,这个结果只是本次测试的结果,不同的机器、不同的数据量、不同的环境下,结果可能会有所不同。 希望这个示例能够帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值