由于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. 搭建自己的SIP服务器:开源sip服务器opensips的搭建及终端TwInkle的使用

    搭建自己的SIP服务器:开源sip服务器opensips的搭建及终端TwInkle的使用 分类: linux编译相关2013-01-05 21:38 17983人阅读 评论(24) 收藏 举报 先下载 ...

  2. 2014-08-07 SSDB 使用 rocksdb 引擎

    http://www.ideawu.net/blog/archives/824.html 为了满足各位对 Facebook 出品的 rocksdb 的爱好, SSDB 数据库也可以使用 rocksdb ...

  3. kettle job如何利用java的反射机制获取执行的sql语句

    kettle job中的JavaScript如何获取同一个job中SQL步骤的执行语句并让执行语句记录在日志中呢?首先写日志需要用到job中JavaScript写日志的方法,其次是利用java反射机制 ...

  4. NEFU 1142 表哥的面包

    表哥的面包 Problem:1142 Time Limit:1000ms Memory Limit:65535K Description 可爱的表哥遇到了一个问题,有一个长为N(1≤N≤10^18)的 ...

  5. sublime快捷键整理

    快捷键 功能 ctrl+shift+n 打开新Sublime ctrl+shift+w 关闭Sublime,关闭所有打开文件 ctrl+shift+t 重新打开最近关闭文件 ctrl+n 新建文件 c ...

  6. dbvisualizer中文乱码

    .dbvisualizer中文乱码 tools--tool properties找到Fonts--修改SQL Editor/Text Editor 将字体换成:微软雅黑.新宋体.楷体.黑体 .dbvi ...

  7. MVC准备前基础知识

    一.自动属性C#自动属性可以避免原来这样我们手工声明一个私有成员变量以及编写get/set逻辑public class Product{ public int Id { get; set; } pub ...

  8. java equals 和 "==" 比较

    java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean   他们之间的比较,应用双等号(== ...

  9. Android文件Apk下载变ZIP压缩包解决方案

    [root@ conf]# pwd /alidata/server/nginx/conf [root@ conf]# vi mime.types application/vnd.android.pac ...

  10. xampp 访问出现New XAMPP security concept

    在浏览器输入 http://60.10.140.22/xampp出现以下错误信息: Access forbidden! New XAMPP security concept: Access to th ...