Difference Between HashMap and IdentityHashMap--转
原文地址: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--转的更多相关文章
- 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 ...
- Java 数据类型:集合接口Map:HashTable;HashMap;IdentityHashMap;LinkedHashMap;Properties类读取配置文件;SortedMap接口和TreeMap实现类:【线程安全的ConcurrentHashMap】
Map集合java.util.Map Map用于保存具有映射关系的数据,因此Map集合里保存着两个值,一个是用于保存Map里的key,另外一组值用于保存Map里的value.key和value都可以是 ...
- How HashMap works in Java
https://www.javainterviewpoint.com/hashmap-works-internally-java/ How a HashMap Works internally has ...
- 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 ...
- Popular HashMap and ConcurrentHashMap Interview Questions
http://howtodoinjava.com/core-java/collections/popular-hashmap-and-concurrenthashmap-interview-quest ...
- [Think In Java]基础拾遗3 - 容器、I/O、NIO、序列化
目录 第十一章 持有对象第十七章 容器深入研究第十八章 Java I/O系统 第十一章 持有对象 1. java容器概览 java容器的两种主要类型(它们之间的主要区别在于容器中每个“槽”保存的元素个 ...
- Java容器之旅:容器基础知识总结
下图展示了Java容器类库的完备图,包括抽象类和遗留构件(不包括Queue的实现). 常用的容器用黑色粗线框表示,点线框表示接口,虚线框表示抽象类,实线框表示类,空心箭头表示实现关系.Produce表 ...
- Java Map 简介
AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, H ...
- java集合类(五)About Map
接上篇“java集合类(四)About Set” 这次学完Map之后,就剩队列的知识,之后有关java集合类的学习就将告一段落,之后可能会有java连接数据库,I/O,多线程,网络编程或Android ...
随机推荐
- iOS开发之记录用户登录状态
iOS开发之记录用户登录状态 我们知道:CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreData还是蛮好用的.今天要说的是如何记录我们用户的登陆状态.例如微信 ...
- Windows多线程多任务设计初步(转)
Windows多线程多任务设计初步 [前言:]当前流行的Windows操作系统,它能同时运行几个程序(独立运行的程序又称之为进程),对于同一个程序,它又可以分成若干个独立的执行流,我们称之为线程,线程 ...
- windows 环境和linux环境下 ping命令的区别:
Ping 是Windows自带的一个DOS命令.利用它可以检查网络是否能够连通,用好它可以很好地帮助我们分析判定网络故障.该命令可以加许多参数使用,键入Ping按回车即可看到详细说明.Ping 命令可 ...
- nodejs学习之加密
Nodejs中的加密是Crypto模块, 1.md5的使用 var crypto = require("crypto"); //创建 var md5 = crypto.create ...
- windows平台,WEB开发,喜欢IE + notepad的同行进来可以看。
用多个浏览器查看自己的page( 平台是notepad) 借助window的批处理,以及系统环境变量. 首先, 写个bat, 一句start %1 %2即可. %1和%2分别表示第一个和第二个参数. ...
- html 5 实现拖放效果
在html5中要实现拖放操作,相对于以前通过鼠标操作实现,要简单得多,数据安全性也更有保障.只需要以下几步即可. 给被拖拽元素添加draggable属性,如果是文件拖放. 在拖拽元素的dragstar ...
- python中实现定时器Timer
实现定时器最简单的办法是就是循环中间嵌time.sleep(seconds), 这里我就不赘述了 # encoding: UTF-8 import threading #Timer(定时器)是Thre ...
- php杂记(一)
1.require_once & require include() 函数会将指定的档案读入并且执行里面的程序 include_once() 与include相同,但只允许一次: requir ...
- 《R in Action》读书笔记(1)
MindMapper 原文件
- S1293和S2220KTV项目结束
1.界面原型(前台的界面搭建一下) 2.数据库 3.架构设计 4.约定的文件抽取 2015年7月20日下午 歌星点歌三界面的联动,数据动态加载 01.点击第一个LIstView,弹出第二个ListVi ...