C# HashTable、HashSet和Dictionary的区别
2016-08-02 12:18
393 查看
C# 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,value);
在哈希表中去除某个key/键值对:HashtableObject.Remove(key);
从哈希表中移除所有元素: HashtableObject.Clear();
判断哈希表是否包含特定键key: HashtableObject.Contains(key);
遍历Hashtable的方法:
Hashtable table = new Hashtable(); //增加元素 Key不能重复 table.Add("小明", new Person() { Name = "小明" }); table.Add("小花", new Person() { Name = "小花" }); //改 改值通过Key来确定元素. table["小明"] = new Person() { Name = "小明明" }; //删除 table.Remove("小明"); table.Clear(); //遍历 //table.Keys; 保存了所有的键的集合 foreach (object obj in table.Keys) { Console.WriteLine(table[obj]); } //table.Values 保存所有的值得集合 foreach (object obj in table.Values) { Console.WriteLine(obj); } // 如果直接遍历Hashtable 每1个遍历项是1个DictionaryEntry对象 这个对象里的Key和Value属性保存了每1项的键值 foreach (Object obj in table) { DictionaryEntry en = (DictionaryEntry)obj; Console.WriteLine(en.Key); } //判断哈希表中是否有指定的键 bool b = table.Contains("小明"); Console.WriteLine(b); // 判断哈希表中是否包含指定的对象 table.ContainsValue(value); table.Count//保存哈希表中元素的个数.
2.HashSet
HashSet类主要是设计用来做高性能集运算的,例如对两个集合求交集、并集、差集等。集合中包含一组不重复出现且无特性顺序的元素,HashSet拒绝接受重复的对象。HashSet的一些特性如下:
a. HashSet中的值不能重复且没有顺序。
b. HashSet的容量会按需自动添加。
HashSet<int> hs = new HashSet<int>(); hs.Add(1); hs.Add(2); hs.Add(3); hs.Remove(3); bool b = hs.Contains(2); if (b) { foreach (int n in hs) { //要进行的操作 } }
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 语句进行保护, 效率大减。
(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]; 4000 } 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); }
相关文章推荐
- C# HashTable、HashSet和Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C#中的HashSet, HashTable, Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C# ArrayList、HashSet、HashTable、List、Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- C# HashTable、HashSet和Dictionary的区别
- c#中Dictionary、ArrayList、Hashtable和数组 Array 的区别
- c#中List、Dictionary、ArrayList、Hashtable和数组的区别
- c#中List、Dictionary、ArrayList、Hashtable和数组的区别是什么?