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

测试代码:

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

        foreach(var kvp in ht)
            Console.WriteLine(kvp.Key);
        Console.WriteLine("============================================");
        Hashtable ht2=new Hashtable();
        ht2.Add("http://www.sina.com.cn", "");
        ht2.Add("http://www.bjut.edu.cn", "");
        ht2.Add("http://lib.bjut.edu.cn", "");
        ht2.Add("http://news.bjut.edu.cn", "");
        ht2.Add("http://sse.bjut.edu.cn", "");
        ht2.Add("http://lexus.cnblogs.com", "");
        ht2.Add("http://www.sina.com.cn/sport", "");
        ht2.Add("http://www.sina.com.cn/ent", "");
        foreach(DictionaryEntry i in ht2)
            Console.WriteLine(i.Key);

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

代码:(转)

using System;
using System.Collections;

namespace NoSortHashtable
{
    /// <summary>
    /// Summary description for NoSortedHashtable.
    /// </summary>
    public class NoSortHashtable : Hashtable
    {
        private ArrayList keys = new ArrayList();

        public NoSortHashtable()
        {
        }
        

        public override void Add(object key, object value)
        {
            base.Add (key, value);
            keys.Add (key);
        }

        public override ICollection Keys
        {
            get
            {
                return keys;
            }
        }

        public override void Clear()
        {
            base.Clear ();
            keys.Clear ();
        }

        public override void Remove(object key)
        {
            base.Remove (key);
            keys.Remove    (key);
        }
        public override IDictionaryEnumerator GetEnumerator()
        {
            return base.GetEnumerator ();
        }

    }
}

测试:

            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>();
        ht.Add("http://www.sina.com.cn","");
        ht.Add("http://www.bjut.edu.cn","");
        ht.Add("http://lib.bjut.edu.cn", "");
        ht.Add("http://news.bjut.edu.cn", "");
        ht.Add("http://sse.bjut.edu.cn", "");
        ht.Add("http://lexus.cnblogs.com", "");
        ht.Add("http://www.sina.com.cn/sport", "");
        ht.Add("http://www.sina.com.cn/ent", "");

foreach(var kvp in ht)
              Console.WriteLine(kvp.Key);

Hashtable和Dictionary<T,K>的使用的更多相关文章

  1. (转)C#中键值对类型Hashtable与Dictionary比较和相关用法

    最近在使用C#中的Hashtable与Dictionary的时候,想知道其区别,通过查找网络相关博客资料,作出下列总结. Hashtable与Dictionary虽然都是作为键值对的载体,但是采用的是 ...

  2. C# 集合类 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)

    我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类.我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和 ...

  3. 深入解析Hashtable、Dictionary、SortedDictionary、SortedList

    我们先看Hashtable. MSDN的解释:表示键/值对的集合,这些键/值对根据键的哈希代码进行组织. Hash算法是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定 ...

  4. C#下Hashtable和Dictionary之间的差别

    Hashtable和Dictionary都是.Net下的表示键值对的集合,那么我们在使用中该选择Hashtable还是Dictionary?下边我们看看他们之间的区别:1.Dictionary< ...

  5. 【C#集合】Hashtable 和 Dictionary的区别

    Hashtable 和 Dictionary <K, V> 类型 1):单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分. 2):Diction ...

  6. C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别

    C#中HashTable.Dictionary.ConcurrentDictionar三者都表示键/值对的集合,但是到底有什么区别,下面详细介绍 一.HashTable HashTable表示键/值对 ...

  7. C#:Hashtable和Dictionary

    Dictionary<TKey, TValue> ()      Hashtable() 第一.存储的数据类型 Hashtable不是泛型的,不是类型安全的:Dictionary是泛型的, ...

  8. Hashtable、Dictionary和List 谁效率更高

    一 前言 很少接触HashTable晚上回来简单看了看,然后做一些增加和移除的操作,就想和List 与 Dictionary比较下存数据与取数据的差距,然后便有了如下的一此测试, 当然我测的方法可能不 ...

  9. C#中Hashtable、Dictionary详解以及写入和读取对比

    转载:http://www.cnblogs.com/chengxingliang/archive/2013/04/15/3020428.html 在本文中将从基础角度讲解HashTable.Dicti ...

随机推荐

  1. php增加对mysqli的支持

    php增加对mysqli的支持   我在fedora下使用yum安装的php和mysql,但是发现php不支持myslqi,只能编译一个mysqli的扩展给php用了. 方法如下: 1.下载php 2 ...

  2. vista/win7/win8区别

    1. Vista的内核版本号是:Windows 6.0:         Windows 7的内核是:Windows 6.1:         Windows 8的内核是:Windows 6.2   ...

  3. Kth Largest Element in an Array

    Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...

  4. DisJSet:食物链(POJ 1182)

           动物王国的食物链 这一题有两种思路,先介绍第一种: 题目是中文的,我就不翻译了,也很好理解,就是一个A-B-C-A的一个循环的食物链,给定一些条件,问你哪些条件是错的 这一题,是一道比较 ...

  5. 【python】a[::-1]翻转

    设有一个元组或者列表 a = (1,2,3,4) b = [1,2,3,4] 则a[::-1]和b[::-1]的含义是将元组或列表的内容翻转 a[::-1] # 结果为(4,3,2,1) b[::-1 ...

  6. MySQL主备库切换(MHA)演练与总结

      演练包括被动切换和主动切换两部分.被动切换是主库宕机,主动切换是人工手动触发.   演练步骤大致如下:       1 先停掉主库,模拟主库宕机     2 mha将vip切到备库,备库变成主库, ...

  7. C/C++函数参数读取顺序2

    #include <stdio.h>int main(){ int a=1,b=3,c=3; printf("%d, %d, %d",(a++,b++,c++),b,c ...

  8. [转]Java Thread Dump 性能分析

    Java and Thread 一个 web 服务器使用几十到几百个线程来处理大量并发用户,如果一个或多个线程使用相同的资源,线程之间的竞争就不可避免了,并且有时候可能会发生死锁. Thread co ...

  9. MySQL数据库的主从同步实现及应用

    >>主从同步机制及应用 读写分离(Read/Write Splitting)让主数据库处理事务性增.改.删操作(INSERT.UPDATE.DELETE),从数据库处理SELECT查询操作 ...

  10. 【小技巧】9针USB转串口简易连通性测试,附25针转9针

    Part 1 前言 最近用SecureCRT连接串口,因为是笔记本用的USB转串口,好多次出现安装驱动OK,连接上了,但是没有串口打印.无法进行控制的问题:所以不清楚是USB串口的驱动问题,还是转接用 ...