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的更多相关文章

  1. .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? ...

  2. Hashtable,HashMap,Dictionary的区别

    Hashtable和HashMap的区别:1.Hashtable是基于Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现,c#中无HashMap2.Hashtable ...

  3. HashTable、HashMap、HashSet

    1. HashMap 1)  hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashmap中put元素的时候,先根据key的hash ...

  4. Hashtable和HashMap的区别举例

    我们先看2个类的定义 public class Hashtable extends Dictionary implements Map, Cloneable, java.io.Serializable ...

  5. Hashtable和HashMap类的区别

    Hashtable和HashMap类有三个重要的不同之处.第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现. ...

  6. Hashtable和HashMap类

    Hashtable和HashMap类有三个重要的不同之处. 第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现 ...

  7. HashTable和HashMap的区别

    1.HashTable线程安全,同步,效率相对低下. HashMap线程不安全,非同步,效率相对高 2.父类:HashTable的父类是Dictionary HashMap是AbstractMap 3 ...

  8. HashTable与HashMap使用总结

    1.HashTable和HashMap比较 1)继承的父类不同. Hashtable继承自Dictionary类,而HashMap继承自AbstractMap类.但二者都实现了Map接口. publi ...

  9. C# Hashtable 使用说明 以及 Hashtable和HashMap的区别

    一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其 ...

随机推荐

  1. Android 手机卫士12--进程管理

    1.本进程不能被选中,所以先将checkbox隐藏掉--手机卫士 不能自杀 if(getItem(position).packageName.equals(getPackageName())){ ho ...

  2. J2EE分布式架构及MySQL交流群

    J2EE分布式架构及MySQL交流群:577913057

  3. IOS网络编请求响应之URL结构

    资料均来自互联网,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. 人魔七七:http://www.cnblogs.com/qiqibo/ 对于我们IOS开发者来说 ...

  4. 认识Runtime2

    我定义了一个Person类作为测试. 其中Person.h: // // Person.h // Test // // Created by zhanggui on 15/8/16. // Copyr ...

  5. 求当前时间100天后的时间日期,格式化为xxxx年xx月xx日

    package com.demo1; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Da ...

  6. IOS杂笔- 7(类方法load与initialize的区别 浅析)

    在介绍两种类方法之前,NSObject Class Reference里对这两个方法说明: +(void)initialize The runtime sends initialize to each ...

  7. c语言的简易日历

    用c语言编写的简易日历,代码如下: #include <stdio.h> int main(int argc, const char * argv[]) { // insert code ...

  8. 网易新闻iOS版使用的18个开源组件

    转载来自:http://www.jianshu.com/p/8952944f7566  原文最后编辑时间:2015.05.19 网易新闻iOS版在开发过程中曾经使用过的第三方开源类库.组件 1.AFN ...

  9. abs()函数的返回值问题

    转载原文地址:http://www.cnblogs.com/webary/p/4967868.html 在牛客网看到一道关于abs()函数返回值的题目,见下图,当时还没反应过来,第一反应是:自从我开始 ...

  10. eclipse 中手动安装 subversive SVN

    为什么我选择手动安装呢?因为通过 eclipse market 下载实在太慢了.   1.下载离线安装包 http://www.eclipse.org/subversive/latest-releas ...