由于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. Having与Where的区别

    where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,where条件中不能包含聚组函数,使用where条件过滤出特定的行. having 子句的作用是筛 ...

  2. mysql中的unsigned

    unsigned   既为非负数,用此类型可以增加数据长度! 例如如果    tinyint最大是127,那    tinyint    unsigned    最大   就可以到    127 * ...

  3. 【转】PowerDesigner使用方法小结

    本文转自:http://www.cnblogs.com/afarmer/archive/2012/11/05/2755327.html PowerDesigner多用来进行数据库模型设计,具有SQL语 ...

  4. Java for LeetCode 058 Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  5. IOS多线程(NSThread)

    1.创建方法 使用NSThread创建线程主要有两个个方法,分别如下 NSThread* myThread = [[NSThread alloc] initWithTarget:self   sele ...

  6. 自定义Notification

    private static void updateProgressNotification(Context cxt, int appsCount, int percent, String appNa ...

  7. 在iOS开发中,给项目添加新的.framework

    首先需要了解一下iOS中静态库和动态库.framework的概念 静态库与动态库的区别 首先来看什么是库,库(Library)说白了就是一段编译好的二进制代码,加上头文件就可以供别人使用. 什么时候我 ...

  8. laravel框架session使用教程

    laravel是一款php框架了,在使用laravel时会碰到session使用问题了,在使用过程中碰到一些问题与一些应用的例子. 用Laravel开发应用,把原有的代码copy过来,以前的代码ses ...

  9. iOS经典面试题

    前言 写这篇文章的目的是因为前两天同学想应聘iOS开发,从网上找了iOS面试题和答案让我帮忙看看.我扫了一眼,倒吸了一口冷气,仔细一看,气的发抖.整篇题目30多个没有一个答案是对的,总结这篇面试题的作 ...

  10. MATLAB学习笔记(二)——主要是MATLAB的矩阵知识

    PS:主要是讲解矩阵的相应的实现方法,其实MATLAB的很大一部分的优势,就是集成了矩阵级别的运算,并以此为特点,可以进行多维空间上的验证. 让我们懂得了原来线性代数如此有用= - =. (一)MAT ...