HashTable Dictionary HashMap
HashTable和HashMap
脑海中一直存在两个Hash,一个是HashMap另一个是HashTable,今天来总结一下两者的区别
相同点:表示根据键的哈希代码进行组织的键/值对的集合,哈希表也叫散列表。
区别:HashMap在C#中不存在的,而是在Java中
1.C#每一个元素都是存储在DictionaryEntry对象中的键/值对,键不能为 null,但值可以。
2.在Java的HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示HashMap中没有该键,也可以表示该键所对应的值为null。
因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键,而应该用containsKey()方法来判断
HashTable示例
using System;
using System.Collections;
namespace MyCollection
{
public class HashTableExample
{
public static void Main()
{
// Create a new hash table.
Hashtable openWith = new Hashtable();
// key没有重复, 但是value有重复.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
//如果key重复,进行catch处理
try
{
openWith.Add("txt", "winword.exe");
}
catch
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}
// 通过key获取value
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
//替换value
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
//遍历HashTable
foreach (DictionaryEntry de in openWith)
{
Console.WriteLine(de.Key);
}
//获取Keys
ICollection keCollection = openWith.Keys;
foreach (string s in keCollection)
{
Console.WriteLine("key = {0}",s);
}
//删除指定的key
openWith.Remove("doc");
if (!openWith.Contains("doc"))
{
Console.WriteLine("Key\"doc\" is not found");
}
}
}
}
运行结果

HashTable和Dictionary
示例代码
using System;
using System.Collections;
using System.Collections.Generic;
namespace MyCollection
{
class HashTableDictionary
{
static void Main(string[] args)
{
Hashtable hashtable = new Hashtable();
hashtable.Add("8","Name8");
hashtable.Add("2", "Name5");
hashtable.Add("5", "Name2");
hashtable.Add("1", "Name1");
foreach (var hash in hashtable.Keys)
{
Console.WriteLine(hash.ToString());
}
Console.WriteLine();
Dictionary<int,string> dict = new Dictionary<int, string>();
dict.Add(8, "Name8");
dict.Add(2, "Name5");
dict.Add(5, "Name2");
dict.Add(1, "Name1");
foreach (var _dict1 in dict.Keys)
{
Console.WriteLine(_dict1);
}
Console.WriteLine();
Dictionary<string, string> dict2 = new Dictionary<string, string>();
dict2.Add("8", "Name8");
dict2.Add("2", "Name5");
dict2.Add("5", "Name2");
dict2.Add("1", "Name1");
foreach (var _dict2 in dict2.Keys)
{
Console.WriteLine(_dict2);
}
}
}
}
运行结果

HashTable Dictionary HashMap的更多相关文章
- .Net 中HashTable,HashMap 和 Dictionary<key,value> 和List<T>和DataTable的比较
参考资料 http://www.cnblogs.com/MichaelYin/archive/2011/02/14/1954724.html http://zhidao.baidu.com/link? ...
- Hashtable,HashMap,Dictionary的区别
Hashtable和HashMap的区别:1.Hashtable是基于Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现,c#中无HashMap2.Hashtable ...
- HashTable、HashMap、HashSet
1. HashMap 1) hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashmap中put元素的时候,先根据key的hash ...
- Hashtable和HashMap的区别举例
我们先看2个类的定义 public class Hashtable extends Dictionary implements Map, Cloneable, java.io.Serializable ...
- Hashtable和HashMap类的区别
Hashtable和HashMap类有三个重要的不同之处.第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现. ...
- Hashtable和HashMap类
Hashtable和HashMap类有三个重要的不同之处. 第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现 ...
- HashTable和HashMap的区别
1.HashTable线程安全,同步,效率相对低下. HashMap线程不安全,非同步,效率相对高 2.父类:HashTable的父类是Dictionary HashMap是AbstractMap 3 ...
- HashTable与HashMap使用总结
1.HashTable和HashMap比较 1)继承的父类不同. Hashtable继承自Dictionary类,而HashMap继承自AbstractMap类.但二者都实现了Map接口. publi ...
- C# Hashtable 使用说明 以及 Hashtable和HashMap的区别
一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其 ...
随机推荐
- MessageBox的Buttons和三级联动
一.MessageBox的Buttons MessageBox.Show可以出现有按钮的对话框 例如: DialogResult dr = MessageBox.Show("是否要继续吗?& ...
- Orchard源码:Logging
试着用markdown写些东西.貌似博客园支持的还有问题,代码片段显示错位,还得另外上传图片.还是用普通方法写写随笔好了. Logging相对也是比较松耦合的模块,可以随时提取出来用在自己的项目中.其 ...
- js实现拖拽
拖拽:最核心是三个事件,鼠标按下,鼠标移动,鼠标弹起.按下时激活拖拽,然后时刻根据鼠标的位置来更新物体的left和top值,达到跟随鼠标的效果,鼠标弹起则取消拖拽. 以下是代码: <!DOCTY ...
- css权重是什么
css权重是什么? 概述 css Specificity中文一般译为css优先级.css权重.相比"权重","优先级"更好理解,mozilla官方中文文档就翻译 ...
- FME2014汉化问题
问题:FME2014汉化包安装上不起作用,安装环境是是Win7 64位,FME是64位版本的,默认位置在Program Files\FME下,而汉化包安装默认位置在Program Files (x86 ...
- ArcEngine环境下合并断开的线要素(根据属性)
1.遇到的问题: 最近遇到在线要素(矢量数据)中,一条完整的道路.河流等往往是断开的,如下图1所示: 2.思路: 在ArcGIS Desktop中没有相关的工具可以将这些断开的线要素进行自动合并,今天 ...
- SarePoint Powershell Add user to Group
$FromGroupnames = "001总经理","010101管理本部" $ToGroupname = "test" $SPWeb = ...
- Python数据结构与算法--算法分析
在计算机科学中,算法分析(Analysis of algorithm)是分析执行一个给定算法需要消耗的计算资源数量(例如计算时间,存储器使用等)的过程.算法的效率或复杂度在理论上表示为一个函数.其定义 ...
- KVC实现原理简介
KVC,全称:Key-Value-Coding. KVC运用了isa-swizzling技术.isa-swizzling就是类型混合指针机制.KVC主要通过isa-swizzling来实现其内部定位查 ...
- (方法调配)Method Swizzling
一.概念 方法调配:因为Objective-C是运行时语言,也就是说究竟会调用何种方法要在运行期才能解析出来.那么我们其实也可以在运行时改变选择子名称.这样我们既不需要查看到源代码,又没有必要去重写子 ...