有关hashtable和dictionary

来源:互联网 发布:软件设计师考试时间 编辑:程序博客网 时间:2024/06/04 19:49

写引擎的时候,对这两个概念把握的不是很好。

HashTable

Hashtable optimizes lookups. It computes a hash of each key you add. It then uses this hash code to look up the element very quickly. It is an older .NET Framework type. It is slower than the genericDictionarytype.

优化了查找。添加的每个key都会进行计算hash。如果之后使用这个hash代码来查找元素非常快。

Example:
using System.Collections;
class Program
{
    static Hashtable GetHashtable()
    {
        // Create and return new Hashtable.
        Hashtable hashtable = new Hashtable();
        hashtable.Add("Area", 1000);
        hashtable.Add("Perimeter", 55);
        hashtable.Add("Mortgage", 540);
        return hashtable;
    }
    public  static void Main()
    {
        Hashtable hashtable = GetHashtable();
        // See if the Hashtable contains this key.
        Console.WriteLine(hashtable.ContainsKey("Perimeter"));
        // Test the Contains method. It works the same way.
        Console.WriteLine(hashtable.Contains("Area"));
        // Get value of Area with indexer.
        int value = (int)hashtable["Area"];
        // Write the value of Area.
        Console.WriteLine(value);
    }
}

Output:

True
True
1000


Dictionary

A dictionary is used where fast look ups are critical. The Dictionary type provides fast lookups with keys to get values. With it we use keys and values of any type, including ints and strings. Dictionary requires a special syntax form.

Dictionary is used when we have many different elements. We specify its key type and its value type. It provides good performance.

Dictionary被用在快速查找。提供了通过keys快速得到值。它的键和值可以使用的任何类型,包括整数和字符串。
词典需要一种特殊的语法形式。

Dictionary用在,最好指定其键类型和值类型。这能提高性能。


Example:

class Dict
{
    static void Main()
    {
       // Example Dictionary again
       Dictionary<string, int> d = new Dictionary<string, int>()
       {
           {"Lion", 2},  {"dog", 1}};
           // Loop over pairs with foreach
           foreach (KeyValuePair<string, int> pair in d)
           {
               Console.WriteLine ("{0}, {1}",pair.Key,  pair.Value);
           }
           foreach (var pair in d)
           {
               Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
           }
           Console.ReadKey();
       }
   }

}

 Dictionary:

    It returns error if we try to find a key which does not exist.
    It is faster than a Hashtable because there is no boxing and unboxing.
    Only public static members are thread safe.
    Dictionary is a generic type which means we can use it with any data type.

Hashtable:

    It returns null if we try to find a key which does not exist.
    It is slower than dictionary because it requires boxing and unboxing.
    All the members in a Hashtable are thread safe,
    Hashtable is not a generic type, 

1):单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分.
2):多线程程序中推荐使用 Hashtable, 默认的 Hashtable 允许单线程写入, 多线程读取, 对 Hashtable 进一步调用 Synchronized()方法可以获得完全线程安全的类型. 而Dictionary 非线程安全, 必须人为使用 lock 语句进行保护, 效率大减.
3):Dictionary 有按插入顺序排列数据的特性 (注: 但当调用 Remove() 删除过节点后顺序被打乱), 因此在需要体现顺序的情境中使用 Dictionary 能获得一定方便.

HashTable中的key/value均为object类型,由包含集合元素的存储桶组成。存储桶是 HashTable中各元素的虚拟子组,与大多数集合中进行的搜索和检索相比,存储桶可令搜索和检索更为便捷。每一存储桶都与一个哈希代码关联,该哈希代码是使用哈希函数生成的并基于该元素的键。HashTable的优点就在于其索引的方式,速度非常快。如果以任意类型键值访问其中元素会快于其他集合,特别是当数据量特别大的时候,效率差别尤其大。

HashTable的应用场合有:做对象缓存,树递归算法的替代,和各种需提升效率的场合。

一,哈希表(Hashtable)

在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中key/value键值对均为object类型,所以Hashtable可以支持任何类型的key/value键值对.

1.1 哈希表的简单操作

在哈希表中添加一个key/value键值对:HashtableObject.Add(key,value);
在哈希表中去除某个key/value键值对:HashtableObject.Remove(key);
从哈希表中移除所有元素: HashtableObject.Clear();
判断哈希表是否包含特定键key: HashtableObject.Contains(key);

哈希表,名-值对。类似于字典(比数组更强大)。哈希表是经过优化的,访问下标的对象先散列过。如果以任意类型键值访问其中元素会快于其他集合。

GetHashCode()方法返回一个int型数据,使用这个键的值生成该int型数据。哈希表获取这个值最后返回一个索引,表示带有给定散列的数据项在字典中存储的位置。

 //Hashtable sample
System.Collections.Hashtable ht = new System.Collections.Hashtable();
//--Be careful: Keys can't be duplicated, and can't be null----
ht.Add(1, "apple");
ht.Add(2, "banana");
ht.Add(3, "orange");
//Modify item value:
if(ht.ContainsKey(1))
ht[1] = "appleBad";
//The following code will return null oValue, no exception
object oValue = ht[5];

1.2  哈希表遍历

遍历Hashtable对象的两种方法:

由于Hashtable每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型,而是DictionaryEntry类型。  

//方法一:遍历traversal 1:
foreach (DictionaryEntry de in ht)
{
Console.WriteLine(de.Key);
Console.WriteLine(de.Value);
}
//方法二:遍历traversal 2:
System.Collections.IDictionaryEnumerator d = ht.GetEnumerator();
while (d.MoveNext())
{
Console.WriteLine("key:{0} value:{1}", d.Entry.Key, d.Entry.Value);
}
//Clear items
ht.Clear();

1.3 排序

HashTable是经过优化的,访问下标的对象先散列过,所以内部是无序散列的,保证了高效率,也就是说,其输出不是按照开始加入的顺序,而Dictionary遍历输出的顺序,就是加入的顺序,这点与Hashtable不同。如果一定要排序HashTable输出,只能自己实现:

 //排序 Hashtable sorting

System.Collections.ArrayList akeys = new System.Collections.ArrayList(ht.Keys); //from Hashtable
akeys.Sort(); //Sort by leading letter
foreach (string skey in akeys)
{
Console.Write(skey + ":");
Console.WriteLine(ht[skey]);
}

1.4、HashTable与线程安全:

为了保证在多线程的情况下的线程同步访问安全,微软提供了自动线程同步的HashTable:

如果 HashTable要允许并发读但只能一个线程写, 要这么创建 HashTable实例:

 //Thread safe HashTable
System.Collections.Hashtable htSyn = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());

这样, 如果有多个线程并发的企图写HashTable里面的 item, 则同一时刻只能有一个线程写, 其余阻塞; 对读的线程则不受影响。

另外一种方法就是使用lock语句,但要lock的不是HashTable,而是其SyncRoot;虽然不推荐这种方法,但效果一样的,因为源代码就是这样实现的:

private static System.Collections.Hashtable htCache = new System.Collections.Hashtable ();

public static void AccessCache ()

{

lock ( htCache.SyncRoot )

{

htCache.Add ( "key", "value" );

}

}

//Is equivalent to 等同于 (lock is equivalent to Monitor.Enter and Exit()

public static void AccessCache ()

{

System.Threading.Monitor.Enter ( htCache.SyncRoot );

try

{

htCache.Add ( "key", "value" );

}

finally

{

System.Threading.Monitor.Exit ( htCache.SyncRoot );

}

}

二 字典

Dictionary<Tkey,Tvalue>是Hastbale的泛型实现。

Dictionary和HashTable内部实现差不多,但前者无需装箱拆箱操作,效率略高一点。

 //Dictionary sample
System.Collections.Generic.Dictionary<int, string> fruits = new System.Collections.Generic.Dictionary<int, string>();
fruits.Add(1, "apple");
fruits.Add(2, "banana");
fruits.Add(3, "orange");
foreach (int i in fruits.Keys)
{
Console.WriteLine("key:{0} value:{1}", i, fruits); }

if (fruits.ContainsKey(1))
{
Console.WriteLine("contain this key.");
}

2.1 字典遍历

//遍历键
foreach (string key in myDictionary.Keys)
{
    //遍历某键的值
    foreach (string val in myDictionary[key])
    {

    }
}

由于 Dictionary 是键和值的集合,因此元素类型并非键类型或值类型。相反,元素类型是键类型和值类型的 KeyValuePair。

foreach (KeyValuePair<string, string> kvp in myDictionary)
{
    string key = kvp.Key;//key包含了字典里的键
    for (int i = 0; i < kvp.Value.Count; i++)
    {
        Response.Write(kvp.Value[i]);
    }
}


装箱是将值类型转换为 object 类型或由此值类型实现的任一接口类型的过程。 当 CLR 对值类型进行装箱时,会将该值包装到 System.Object 内部,再将后者存储在托管堆上。拆箱将从对象中提取值类型。 相对于简单的赋值而言,装箱和取消装箱过程需要进行大量的计算。 对值类型进行装箱时,必须分配并构造一个新对象。 次之,拆箱所需的强制转换也需要进行大量的计算。
装箱用于在堆中存储值类型。 装箱是值类型到 object 类型或到此值类型所实现的任何接口类型的隐式转换。 对值类型装箱会在堆中分配一个对象实例,并将该值复制到新的对象中。 整型变量 i 装箱并赋给对象 o:原始值类型和装箱的对象使用不同的内存位置,因此能够存储不同的值。

0 0
原创粉丝点击