Hashtable和Dictionary的使用

由于Hashtable内部自带有排序(根据Key的HashCode来进行的),因此有时在使用Hashtable时就会造成数据顺序不可控的情况,有两种办法可以解决,

测试代码:

 Dictionary<string,string> ht=new Dictionary<string, string>();
Hashtable和Dictionary<T,K>的使用        ht.Add("http://www.sina.com.cn","");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://www.bjut.edu.cn","");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://lib.bjut.edu.cn", "");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://news.bjut.edu.cn", "");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://sse.bjut.edu.cn", "");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://lexus.cnblogs.com", "");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://www.sina.com.cn/sport", "");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://www.sina.com.cn/ent", "");
Hashtable和Dictionary<T,K>的使用
Hashtable和Dictionary<T,K>的使用        foreach(var kvp in ht)
Hashtable和Dictionary<T,K>的使用            Console.WriteLine(kvp.Key);
Hashtable和Dictionary<T,K>的使用        Console.WriteLine("============================================");
Hashtable和Dictionary<T,K>的使用        Hashtable ht2=new Hashtable();
Hashtable和Dictionary<T,K>的使用        ht2.Add("http://www.sina.com.cn", "");
Hashtable和Dictionary<T,K>的使用        ht2.Add("http://www.bjut.edu.cn", "");
Hashtable和Dictionary<T,K>的使用        ht2.Add("http://lib.bjut.edu.cn", "");
Hashtable和Dictionary<T,K>的使用        ht2.Add("http://news.bjut.edu.cn", "");
Hashtable和Dictionary<T,K>的使用        ht2.Add("http://sse.bjut.edu.cn", "");
Hashtable和Dictionary<T,K>的使用        ht2.Add("http://lexus.cnblogs.com", "");
Hashtable和Dictionary<T,K>的使用        ht2.Add("http://www.sina.com.cn/sport", "");
Hashtable和Dictionary<T,K>的使用        ht2.Add("http://www.sina.com.cn/ent", "");
Hashtable和Dictionary<T,K>的使用        foreach(DictionaryEntry i in ht2)
Hashtable和Dictionary<T,K>的使用            Console.WriteLine(i.Key);

第一种是继承Hashtable,自己创建一个新的类,用一个ArrayList对象保存keys;

代码:(转)

using System;
Hashtable和Dictionary<T,K>的使用using System.Collections;
Hashtable和Dictionary<T,K>的使用
Hashtable和Dictionary<T,K>的使用namespace NoSortHashtable
}

测试:

            hashTable = new NoSortHashtable();

            hashTable.Add("hunan","changsha");
            hashTable.Add("beijing","beijing");
            hashTable.Add("anhui","hefei");
            hashTable.Add("sichuan","chengdu");
            foreach(string str in hashTable.Keys)
            {
                Console.WriteLine(str + " : " + hashTable[str]);
            }

----------------------------------------------------------------------

第二种办法是采用泛型的Dictionary<T,K>对象,该对象按照插入的顺序输出;

         Dictionary<string,string> ht=new Dictionary<string, string>();
Hashtable和Dictionary<T,K>的使用        ht.Add("http://www.sina.com.cn","");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://www.bjut.edu.cn","");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://lib.bjut.edu.cn", "");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://news.bjut.edu.cn", "");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://sse.bjut.edu.cn", "");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://lexus.cnblogs.com", "");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://www.sina.com.cn/sport", "");
Hashtable和Dictionary<T,K>的使用        ht.Add("http://www.sina.com.cn/ent", "");

          foreach(var kvp in ht)
Hashtable和Dictionary<T,K>的使用              Console.WriteLine(kvp.Key);