由于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. cookie注入讲解

    我们首先还是来看看中网景论坛的最新版本"(CNKBBS2007)中网景论坛2007v5.0 "官方下载地址" http://www.cnetking.com/websys ...

  2. SpringMVC请求处理流程

    从web.xml中 servlet的配置开始, 根据servlet拦截的url-parttern,来进行请求转发   Spring MVC工作流程图   图一   图二    Spring工作流程描述 ...

  3. Convert Sorted List to Balanced BST

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  4. cocos2dx之lua派生类和方法重新

    记得把extern.lua拷贝到你的资源目录,这里要用到 require "extern" MyLayer = class("MyLayer", functio ...

  5. 第十一章 TClientDataSet

    第十一章 TClientDataSet 与TTable.TQuery一样,TClientDataSet也是从TDataSet继承下来的,它通常用于多层体系结构的客户端.TClientDataSet最大 ...

  6. DP:Dollar Dayz(POJ 3181)

    一道高精度DP 题目大意,换工具,有m块钱,有k种价值的物品,(1...k),求一共有多少种换法 这一题就是完全背包,现在这种完全背包对我来说就是水题了, 状态转移方程闭着眼睛写dp[j]+=dp[j ...

  7. codeforces 483A. Counterexample 解题报告

    题目链接:http://codeforces.com/problemset/problem/483/A 题目意思:给出一个区间 [l, r],要从中找出a, b, c,需要满足 a, b 互质,b, ...

  8. Java中的内存分配机制

    Java的内存分为两种:一种是栈内存,一种是堆内存. 在函数中定义的一些基本类型变量和对象的引用都在函数的栈内存中分配.当在一个代码块中定义一个变量的时候,java就在栈中为其分配内存,当超过作用域的 ...

  9. XP下使用IIS访问asp出现无权查看网页问题的解决办法

    这是用户权限问题,因为你用的磁盘是NTFS格式. 解决方法: 一.添加用户(“Everyone”或者“IUSR_你的机器名”,如IUSR_HONG,即“Internet 来宾用户”) 二.修改用户权限 ...

  10. LightOJ 1247 Matrix Game (尼姆博弈)

    A - Matrix Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submi ...