Hashtable和Dictionary<T,K>的使用
由于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.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>的使用的更多相关文章
- (转)C#中键值对类型Hashtable与Dictionary比较和相关用法
最近在使用C#中的Hashtable与Dictionary的时候,想知道其区别,通过查找网络相关博客资料,作出下列总结. Hashtable与Dictionary虽然都是作为键值对的载体,但是采用的是 ...
- C# 集合类 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)
我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类.我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和 ...
- 深入解析Hashtable、Dictionary、SortedDictionary、SortedList
我们先看Hashtable. MSDN的解释:表示键/值对的集合,这些键/值对根据键的哈希代码进行组织. Hash算法是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定 ...
- C#下Hashtable和Dictionary之间的差别
Hashtable和Dictionary都是.Net下的表示键值对的集合,那么我们在使用中该选择Hashtable还是Dictionary?下边我们看看他们之间的区别:1.Dictionary< ...
- 【C#集合】Hashtable 和 Dictionary的区别
Hashtable 和 Dictionary <K, V> 类型 1):单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分. 2):Diction ...
- C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别
C#中HashTable.Dictionary.ConcurrentDictionar三者都表示键/值对的集合,但是到底有什么区别,下面详细介绍 一.HashTable HashTable表示键/值对 ...
- C#:Hashtable和Dictionary
Dictionary<TKey, TValue> () Hashtable() 第一.存储的数据类型 Hashtable不是泛型的,不是类型安全的:Dictionary是泛型的, ...
- Hashtable、Dictionary和List 谁效率更高
一 前言 很少接触HashTable晚上回来简单看了看,然后做一些增加和移除的操作,就想和List 与 Dictionary比较下存数据与取数据的差距,然后便有了如下的一此测试, 当然我测的方法可能不 ...
- C#中Hashtable、Dictionary详解以及写入和读取对比
转载:http://www.cnblogs.com/chengxingliang/archive/2013/04/15/3020428.html 在本文中将从基础角度讲解HashTable.Dicti ...
随机推荐
- 【云计算】基于Ansible的自动部署平台化思路
目标: 1.自动化—支持命令行.webui.api调用 2.支持基础命令.脚本.复杂任务编排.满足生产环境各类模块自动化部署需求 3.满足生产环境(开发.测试.线上)性能.可靠性.安全性要求 4.流程 ...
- Java for LeetCode 025 Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- extjs插件开发上传下载文件简单案例
前台,extjs,框架,mybatis,spring,springMVC,简单的文件上传下载案例. 必要的jar包,commons-fileupload-1.3.1.jar,commons-io-2. ...
- 【HTTP协议】响应头中的Content-Length和Transfer-Encoding
来源: http://blog.csdn.net/superhosts/article/details/8737434 http://bbs.csdn.net/topics/390384017 对于h ...
- 【python】lxml查找属性为指定值的节点
假设有如下xml在/home/abc.xml位置 <A> <B id=" name="apple"/> <B id=" name= ...
- Ubuntu13.04 安装 chrome
1.chrome官网下载deb安装包:https://www.google.com/intl/zh-CN/chrome/browser/ 2.进入下载好的目录执行:sudo dpkg -i googl ...
- iOS开发网络篇—网络请求(HTTP协议)小结(转)
1. 聊一下HTTP协议(协议的完整的通信过程) 2.通信过程 1> 请求 * 客户端 --> 服务器 * 请求的内容 a. 请求行(请求方法\HTTP协议\请求资源路径) b. 请求头( ...
- .net学习之类与对象、new关键字、构造函数、常量和只读变量、枚举、结构、垃圾回收、静态成员、静态类等
1.类与对象的关系类是对一类事务的统称,是抽象的,不能拿来直接使用,比如汽车,没有具体指哪一辆汽车对象是一个具体存在的,看的见,摸得着的,可以拿来直接使用,比如我家的那辆刚刚买的新汽车,就是具体的对象 ...
- Xamarin.Android开发实践(十五)
Xamarin.Android学习之应用程序首选项 一.前言 任何App都会存在设置界面,如果开发者利用普通控件并绑定监听事件保存设置,这 一过程会非常的枯燥,而且耗时.我们可以看到Android系统 ...
- Java Hour 34 Weather ( 7 ) struts2 – validate
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 34 Form Validation 一般Form 提交都有验证的, ...