zoukankan      html  css  js  c++  java
  • HashTable、HashSet和Dictionary的区别(转载)

    1.HashTable
    哈希表(HashTable)表示键/值对的集合。在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key-value的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中key-value键值对均为object类型,所以Hashtable可以支持任何类型的keyvalue键值对,任何非 null 对象都可以用作键或值。
    在哈希表中添加一个key/键值对:HashtableObject.Add(key,); 
    在哈希表中去除某个key/键值对:HashtableObject.Remove(key);
    从哈希表中移除所有元素: HashtableObject.Clear();
    判断哈希表是否包含特定键key: HashtableObject.Contains(key);
      
    2.HashSet
    HashSet<T>类主要是设计用来做高性能集运算的,例如对两个集合求交集、并集、差集等。集合中包含一组不重复出现且无特性顺序的元素,HashSet拒绝接受重复的对象。
      
    HashSet<T>的一些特性如下:
       a. HashSet<T>中的值不能重复且没有顺序。
       b. HashSet<T>的容量会按需自动添加。
      
    3.Dictionary
    Dictionary表示键和值的集合。Dictionary<string, string>是一个泛型,他本身有集合的功能有时候可以把它看成数组
    他的结构是这样的:Dictionary<[key], [value]>
    他的特点是存入对象是需要与[key]值一一对应的存入该泛型
    通过某一个一定的[key]去找到对应的值
      
    4.HashTable和Dictionary的区别:

    1.HashTable不支持泛型,而Dictionary支持泛型。

    2.Hashtable 的元素属于 Object 类型,所以在存储或检索值类型时通常发生装箱和拆箱的操作,所以你可能需要进行一些类型转换的操作,而且对于int,float这些值类型还需要进行装箱等操作,非常耗时。

    3.单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分。多线程程序中推荐使用 Hashtable, 默认的 Hashtable 允许单线程写入, 多线程读取, 对 Hashtable 进一步调用 Synchronized() 方法可以获得完全线程安全的类型. 而 Dictionary 非线程安全, 必须人为使用 lock 语句进行保护, 效率大减。

    关于HashTable的线程安全,微软MSDN的解释如下:

    Hashtable is thread safe for use by multiple reader threads and a single writing thread. It is thread safe for multi-thread use when only one of the threads perform write (update) operations, which allows for lock-free reads provided that the writers are serialized to the Hashtable. To support multiple writers all operations on the Hashtable must be done through the wrapper returned by the Synchronized(Hashtable) method, provided that there are no threads reading the Hashtable object. 

    4.在通过代码测试的时候发现key是整数型Dictionary的效率比Hashtable快,如果key是字符串型,Dictionary的效率没有Hashtable快。

    static void IntMethod()
            {
                int count = 1000000;
                Dictionary<int, int> dictionary = new Dictionary<int, int>();
                Hashtable hashtable = new Hashtable();
                for (int i = 0; i < count; i++)
                {
                    dictionary.Add(i,i);
                    hashtable.Add(i,i);
                }
    
                Stopwatch stopwatch = Stopwatch.StartNew();
                for (int i = 0; i < count; i++)
                {
                    int value = dictionary[i];
                }
                stopwatch.Stop();
                Console.WriteLine(stopwatch.ElapsedMilliseconds);
    
                stopwatch = Stopwatch.StartNew();
                for (int i = 0; i < count; i++)
                {
                    object value = hashtable[i];
                }
                stopwatch.Stop();
    
                Console.WriteLine(stopwatch.ElapsedMilliseconds);
     
            }
    
            static void MethodString()
            {
                int count = 1000000;
                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                Hashtable hashtable=new Hashtable();
                for (int i = 0; i < count; i++)
                {
                    dictionary.Add(i.ToString(),"aaa");
                    hashtable.Add(i.ToString(),"aaa");
                }
    
                Stopwatch stopwatch = Stopwatch.StartNew();
                for (int i = 0; i < count; i++)
                {
                    string value=dictionary[i.ToString()];
                }
                stopwatch.Stop();
                Console.WriteLine(stopwatch.ElapsedMilliseconds);
    
                stopwatch = Stopwatch.StartNew();
                for (int i = 0; i < count; i++)
                {
                    object value = hashtable[i.ToString()];
                }
                stopwatch.Stop();
    
                Console.WriteLine(stopwatch.ElapsedMilliseconds);
            }

    微软HashTable介绍链接

    原文链接

  • 相关阅读:
    Java流关闭总结
    Too many open files 问题
    oracle数据库表被锁的解锁方法
    中文转换成字节数组
    java接口理解
    最小的K个数
    数组中出现次数超过一半的数字
    复杂链表的复制
    二叉树中和为某一值的路径
    二叉搜索树的后序遍历序列
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/10028499.html
Copyright © 2011-2022 走看看