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 ...
随机推荐
- 二级域名session 共享方案
二级域名session 共享方案 1.利用COOKIE存放session_id(); 实例: 域名一文件php代码: <?php session_start(); setcookie(&qu ...
- 细微之处:比较两种CSS清除浮动的兼容
http://www.cnblogs.com/bienfantaisie/archive/2011/05/27/2059597.html 清除浮动是连续浮动元素之后的必备工作,在工作中我做到需要清除浮 ...
- 【Markdown】notepad++ 支持 markdown语法、预览
Notepad++中支持Markdown 最近在学习Markdown语言的使用,很想在XP主机上使用Markdown的离线编辑器,但MarkdownPad.作业部分的离线客户端都不能再XP上运行, ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- Jump Game | & ||
Jump Game | Given an array of non-negative integers, you are initially positioned at the first index ...
- XShell上传下载命令
参考:https://www.centos.bz/2012/12/xshell-securecrtrz-sz-upload-download/ 上传文件时,执行rz就会弹出文件选择对话框来选择文件.下 ...
- Java for LeetCode 038 Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- DP:Apple Catching(POJ 2385)
牛如何吃苹果 问题大意:一个叫Bessie的牛,可以吃苹果,然后有两棵树,树上苹果每分钟会掉一个,这只牛一分钟可以在两棵树中往返吃苹果(且不吃地上的),然后折返只能是有限次W,问你这只叫Bessie的 ...
- [Android Pro] 利用tcpdump和wireshark对android网络请求进行分析
一: tcpdump操作流程 1. 手机要有root权限 2. 下载tcpdump http://www.strazzere.com/android/tcpdump 3. adb push c:\ ...
- 案例(JQuery的ajax无刷新评论)
CommentsTest.html代码: <head> <meta http-equiv="Content-Type" content="text/ht ...