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 ...
随机推荐
- PHP中冒号、endif、endwhile、endfor这些都是什么
我们经常在wordpress一类博客程序的模板里面看到很多奇怪的PHP语法,比如:<?php if(empty($GET_['a'])): ?><font color="r ...
- squid日志配置与轮询
squid日志分类及参数 SQUID默认的log文件非常多,其中最重要的LOG日志有三个,分别为access.log.store.log.cache.log.三个日志的记录的内容如下: access. ...
- LVS-三种负载均衡方式比较
1.什么是LVS? 首 先简单介绍一下LVS (Linux Virtual Server)到底是什么东西,其实它是一种集群(Cluster)技术,采用IP负载均衡技术和 基于内容请求分发技术.调度器具 ...
- 《ASP.NET1200例》<asp:DataList>分页显示图片
aspx页面代码 <asp:DataList ID="dlPhoto" runat="server" Height="137px" W ...
- Django对静态文件的处理——部署阶段
参考:http://blog.makto.me/post/2012-11-09/static-files-in-django-deployment HTML模板中的用法: {% load static ...
- 43. 动态规划求解n个骰子的点数和出现概率(或次数)[Print sum S probability of N dices]
[题目] 把N个骰子扔在地上,所有骰子朝上一面的点数之和为S.输入N,打印出S的所有可能的值出现的概率. [分析] 典型的动态规划题目. 设n个骰子的和为s出现的次数记为f(n,s),其中n=[1-N ...
- 升级Windows10后Apache服务器启动失败的解决方法
升级windows10系统后,微软内置了ASP.NET的web高级服务,默认安装了IIS服务器和MSSQL数据库,因为80端口被占用的原因,导致Apache服务器无法正常启动,但是MySQL服务一切正 ...
- CodeForces - 426B(对称图形)
Sereja and Mirroring Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64 ...
- sublime快捷键整理
快捷键 功能 ctrl+shift+n 打开新Sublime ctrl+shift+w 关闭Sublime,关闭所有打开文件 ctrl+shift+t 重新打开最近关闭文件 ctrl+n 新建文件 c ...
- 《数学之美》(吴军 著)读书笔记:第1章 文字和语言 vs 数字和信息
第1章有4个小节,以及前言. 前言 1.信息 2.文字和数字 3.文字和语言背后的数学 4.小结 下面我一一展开,让我们看看每一节都说了什么. 前言 语言和数字都是信息传播的载体,他们之间其实存在着天 ...