• HashTable、HashSet和Dictionary的区别(转)


    今天又去面试了,结果依然很悲催,平时太过于关注表面上的东西,有些实质却不太清楚,遇到HashTable和Dictionary相关的知识,记录下来,希望对后来人有所帮助,以及对自己以后复习可以参考。

    1.HashTable

      (color{red}{哈希表(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

      (color{red}{HashSet<T>类主要是设计用来做高性能集运算的}),例如对两个集合求交集、并集、差集等。集合中包含一组不重复出现且无特性顺序的元素,(color{red}{HashSet拒绝接受重复的对象})

      HashSet的一些特性如下:

      a. HashSet中的(color{red}{值不能重复且没有顺序})

      b. HashSet(color{red}{容量会按需自动添加})

    3.Dictionary

      (color{red}{Dictionary表示键和值的集合})

      Dictionary<string, string>是一个泛型

      他本身有集合的功能有时候可以把它看成数组

      他的结构是这样的:Dictionary<[key], [value]>

      他的特点是存入对象是需要与[key]值一一对应的存入该泛型

      通过某一个一定的[key]去找到对应的值

    4.HashTable和Dictionary的区别:

    • (1).(color{red}{HashTable不支持泛型}),而(color{red}{Dictionary支持泛型})

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

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

    • (4)在通过代码测试的时候发现(color{red}{key是整数型Dictionary的效率比Hashtable快}),如果(color{red}{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);
            }
    

    转自:https://www.cnblogs.com/akwwl/p/3680376.html

    --欢迎大家来交流反馈。
  • 相关阅读:
    [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
    [LeetCode] Number of Segments in a String 字符串中的分段数量
    [LintCode] Longest Common Prefix 最长共同前缀
    [LintCode] Product of Array Except Self 除本身之外的数组之积
    [LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
    [LintCode] Sort List 链表排序
    [LintCode] Find Peak Element 求数组的峰值
    [LintCode] Container With Most Water 装最多水的容器
    [LintCode] Linked List Cycle 单链表中的环
    [LeetCode] 465. Optimal Account Balancing 最优账户平衡
  • 原文地址:https://www.cnblogs.com/hhyl/p/13603499.html
Copyright © 2020-2023  润新知