原文地址:https://dzone.com/articles/difference-between-hashmap-and

Most of the time I use HashMap whenever a map kinda object is needed. When reading some blog I came across IdentityHashMapin Java. It is good to understand the differences between the two because you never know when you will see them flying across your code and you trying to find out why is  this kinda Map is used here.

IdentityHashMap as name suggests uses the equality operator(==) for comparing the keys. So when you put any Key Value pair in it the Key Object is compared using == operator.

import java.util.HashMap; 
import java.util.IdentityHashMap;
import java.util.Map; public class IdentityMapDemo { public static void main(String[] args) {
Map identityMap = new IdentityHashMap();
Map hashMap = new HashMap();
identityMap.put("a", 1);
identityMap.put(new String("a"), 2);
identityMap.put("a", 3);
hashMap.put("a", 1);
hashMap.put(new String("a"), 2);
hashMap.put("a", 3);
System.out.println("Identity Map KeySet Size :: " + identityMap.keySet().size());
System.out.println("Hash Map KeySet Size :: " + hashMap.keySet().size());
}
}

On the other hand HashMap uses equals method to determine the uniqueness of the Key.

 
k1.equals(k2)
 

instead of equality operator.

When you run the above code the result will be

 
Identity Map KeySet Size :: 2

Hash Map KeySet Size :: 1
 

The Keysize of Identity Map is 2 because here a and new String(“a”) are considered two different Object. The comparison is done using == operator.

For HashMap the keySize is 1 because K1.equals(K2) returns true for all three Keys and hence it keep on removing the old value and updating it with the new one.

These both Maps will behave in same manner if they are used for Keys which are user defined Object and doesn’t overrides equals method.

Difference Between HashMap and IdentityHashMap--转的更多相关文章

  1. Difference between HashMap and Hashtable | HashMap Vs Hashtable

    Both the HashMap and Hashtable implement the interface java.util.Map but there are some slight diffe ...

  2. Java 数据类型:集合接口Map:HashTable;HashMap;IdentityHashMap;LinkedHashMap;Properties类读取配置文件;SortedMap接口和TreeMap实现类:【线程安全的ConcurrentHashMap】

    Map集合java.util.Map Map用于保存具有映射关系的数据,因此Map集合里保存着两个值,一个是用于保存Map里的key,另外一组值用于保存Map里的value.key和value都可以是 ...

  3. How HashMap works in Java

    https://www.javainterviewpoint.com/hashmap-works-internally-java/ How a HashMap Works internally has ...

  4. Difference Between Arraylist And Vector : Core Java Interview Collection Question

    Difference between Vector and Arraylist is the most common  Core Java Interview question you will co ...

  5. Popular HashMap and ConcurrentHashMap Interview Questions

    http://howtodoinjava.com/core-java/collections/popular-hashmap-and-concurrenthashmap-interview-quest ...

  6. [Think In Java]基础拾遗3 - 容器、I/O、NIO、序列化

    目录 第十一章 持有对象第十七章 容器深入研究第十八章 Java I/O系统 第十一章 持有对象 1. java容器概览 java容器的两种主要类型(它们之间的主要区别在于容器中每个“槽”保存的元素个 ...

  7. Java容器之旅:容器基础知识总结

    下图展示了Java容器类库的完备图,包括抽象类和遗留构件(不包括Queue的实现). 常用的容器用黑色粗线框表示,点线框表示接口,虚线框表示抽象类,实线框表示类,空心箭头表示实现关系.Produce表 ...

  8. Java Map 简介

    AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, H ...

  9. java集合类(五)About Map

    接上篇“java集合类(四)About Set” 这次学完Map之后,就剩队列的知识,之后有关java集合类的学习就将告一段落,之后可能会有java连接数据库,I/O,多线程,网络编程或Android ...

随机推荐

  1. mac与windows通过ftp传输文件

    1.两个系统相互传文件,比较通用的方式是用QQ,两台电脑一台各登陆一个qq,发文件就行了,在同一个网段时,qq会自动转换为按局域网的方式传输. 2.本人不愿安装qq,以ftp方式进行传输,先在wndo ...

  2. Django models知识小点

    django 为使用一种新的方式,即关系对象映射(ORM) 一,创建表 1,基本结构 注意: 1,创建标的时候,如果我们不给表加自增列,生成表的时候会默认给我们生成一列为ID的自增列,当然我们也可以自 ...

  3. Spring MVC的配置文件(XML)的几个经典案列

    1.既然是配置文件版的,那配置文件自然是必不可少,且应该会很复杂,那我们就以一个一个的来慢慢分析这些个经典案列吧! 01.实现Controller /* * 控制器 */ public class M ...

  4. linux安装VMware-tools,

    系统中可能预装了open-vm-tools和VMware-tools冲突,所以需要先将前者卸载在进行安装不同系统卸载使用的命令不一样,centos的命令可以使用rpm,ubuntu的命令可以使用dpk ...

  5. ubuntu14.04上Virtualbox安装win7(使用Ghost镜像安装,启用USB设备支持,设置共享目录)

    由于某些软件只有windows版本,于是只好安装个虚拟机win7 /**************************安装*************************************/ ...

  6. angular----关于注入HTML

    后台接口返回了一个HTML字符串,要绑定到页面,第一时间想到了innerHTML... 一:先说下一般用法 //原生用法 <div id="content"></ ...

  7. C# winfrom HttpWebRequest 请求获取html网页信息和提交信息

    string result =GetRequest("http://localhost:32163/DuoBao/ajax.aspx", "time=5"); ...

  8. '-[__NSCFString stringFromMD5]: unrecognized selector sent to instance 0x14d89a50'

    类型:ios 问题描述: 导入百度地图 然后在模拟器运行可以,真机测试不行: 报错: '-[__NSCFString stringFromMD5]: unrecognized selector sen ...

  9. MSBuild的简单介绍与使用

    MSBuild 是 Microsoft 和 Visual Studio的生成系统.它不仅仅是一个构造工具,应该称之为拥有相当强大扩展能力的自动化平台.MSBuild平台的主要涉及到三部分:执行引擎.构 ...

  10. mysql字符串处理例子

    项目中用到的,要判断表中某个字段的某几位,若为某个值则替换,用到了几个典型的字符串操作,记录备注实现方案如下: 备注:如果替代字符串是唯一的话,可以用replace,这里用的是concat拼接. DE ...