原文地址: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. Spring MVC篇二、使用JdbcTemplate进行数据库操作

    上一篇只是一个简单的Spring MVC框架,接下来添加一些跟数据库的交互. 一.添加jdbc相关配置   在maven中添加相关依赖后,配置数据库访问参数及数据源.数据库参数使用配置文件,代码如下: ...

  2. 程序后端调用post请求。

    /// <summary> /// 调用Web API /// </summary> public class InvokeMVCWebAPI { /// <summar ...

  3. python中非关键字可变长参数和关键字变量参数的区别

    #非关键字可变长参数 def add(*arg):    return type(arg) print add() #打印结果 <type 'tuple'> #关键字变量参数 def ab ...

  4. VS2012中丢失ArcGIS模板的解决方法

    VS2012中丢失ArcGIS模板的解决方法 由于ArcGIS10.0(for .NET)默认是用VS2010作为开发工具的,所以在先安装VS2012后装ArcGIS10.0 桌面版及ArcObjec ...

  5. 丰富“WinForms” 的一个别样"项目"(学生管理)

    一个别样的WinForms项目,他并没多么的新颖,但是它的用处确实有点多,或许会有你需要的地方:如果你对WinForms中那么多控件无法把握,又或者是你根本就不懂,那我觉得你应该好好看看,如果一个人的 ...

  6. 手把手教android studio中安装Android Butterknife Zelezny (转)

    原文地址:http://blog.csdn.net/xin917480852/article/details/51176524 用来快速生成findViewById() 安装方法: 打开Android ...

  7. 关于[super dealloc]

    销毁一个对象时,需要重写系统的dealloc方法来释放当前类所拥有的对象,在dealloc方法中需要先释放当前类中所有的对象,然后再调用[super dealloc]释放父类中所拥有的对象.如先调用[ ...

  8. 关于c++的 vector 容器的使用及创建方法

    1.vector向量容器的使用,vector具有自动管理的功能,可以进行元素的查找删除 创建方法: (1)  vector<int > v; 创建了一个v的容器,没指定容量: (2)  v ...

  9. 利用JNI技术在Android中调用、调试C++代码

    参考:http://blog.micro-studios.com/?p=4212 代码:http://pan.baidu.com/s/1sjukSDf

  10. .NET Core 构建配置文件从 project.json 到 .csproj

    从 .NET Core SDK 1.0 Preview 3 build 004056 开始,.NET Core 弃用 project.json,回归 .csproj,主要原因是为了兼容 MSBuild ...