首页 技术 正文
技术 2022年11月9日
0 收藏 708 点赞 2,924 浏览 2128 个字

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 HashMap

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289