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的键值对,其 ...
随机推荐
- Failed to install on device ‘emulator-5554′: timeout
启动android模拟器时候如果提示:Failed to install on device ‘emulator-5554′: timeout 这是可能因为卡的原因导致启动超时,解决办法:eclips ...
- C标准库<signal.h>实现
本文地址:http://www.cnblogs.com/archimedes/p/c-library-signal.html,转载请注明源地址. 背景知识 signal.h是C标准函数库中的信号处理部 ...
- UIScrollView常见属性
什么是UIScrollView •设备的屏幕大小是极其有限的,因此直接展示在用户眼前的内容也相当有限 • •当展示的内容较多,超出一个屏幕时,用户可通过滚动手势来查看屏幕以外的内容 • •普通的UIV ...
- Retrofit源码设计模式解析(下)
本文将接着<Retrofit源码设计模式解析(上)>,继续分享以下设计模式在Retrofit中的应用: 适配器模式 策略模式 观察者模式 单例模式 原型模式 享元模式 一.适配器模式 在上 ...
- 在MAC下搭建JSP开发环境
1.Mac下JDK的下载安装及配置 在安装jdk之后,需要为jdk安装目录配置环境变量: 任意打开终端,默认是家目录的,然后直接输入: touch .bash_profile 然后输入:vi .bas ...
- VIP - virtual IP address
virtual IP address (虚拟 IP 地址)1.是集群的ip地址,一个vip对应多个机器2.与群集关联的唯一 IP 地址 see wiki: A virtual IP address ( ...
- 同域iframe的高度自适应
引子 父页面里控制子页面 子页面里控制父页面 一.引子 我们先看一个示例,有两个页面,1.html通过iframe嵌入2.html,两个页面都是同域的 1.html <!DOCTYPE html ...
- 开源项目go2o - golang版的o2o项目
发一个github上唯一用golang实现的o2o项目 What's Go2o Golang combine simple o2o DDD domain-driven design realizati ...
- Stack与Heap的区别
申明:这里所说的栈和堆是程序内存管理中的栈和堆,而不是数据结构里的栈和堆. (1)保存的内容不同:栈里保存的是局部变量,而堆里保存的是动态申请的变量. (2)栈里的内存系统自动申请和释放,程序执行出申 ...
- Mysql中的函数
什么是函数 mysql中的函数与存储过程类似,都是一组SQL集: 与存储过程的区别 函数可以return值,存储过程不能直接return,但是有输出参数可以输出多个返回值: 函数可以嵌入到sql语句中 ...