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 ...
随机推荐
- 有状态Bean和无状态Bean的定义
有状态会话bean :每个用户有自己特有的一个实例,在用户的生存期内,bean保持了用户的信息,即“有状态”:一旦用户灭亡(调用结束或实例结束),bean的生命期也告结束.即每个用户最初都会得到一个初 ...
- Java反编译代码对齐
使用反编译的代码作为jar包源码进行调试时,经常会遇到的情况是反编译后的源码之在注释里包含行号,但是与代码所在行经常对应不上.这个时候,就有必要对代码进行对齐了. public class Reo ...
- js实现div居中
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Unity3D 导航网格自动寻路(Navigation Mesh)
NavMesh(导航网格)是3D游戏世界中用于实现动态物体自动寻路的一种技术,将游戏中复杂的结构组织关系简化为带有一定信息的网格,在这些网格的基础上通过一系列的计算来实现自动寻路..导航时,只需要给导 ...
- android布局实践——模仿微信主界面
这是目前微信6.0版本的主界面 先来分析一波: 1.(top.xml)界面头部有一个微信(6)消息提醒 一个搜索图标 一个更多的的图标+,中间还有一段空白,我们可以弄两个textView(其 ...
- wamp环境 安装memcache 扩展
这两天在研究tp的memcached缓存 总是遇到坑 在网上找了很多教程看终于弄出来了现在拿出来分享 首先安装memcached下载memcache压缩包 使用cmd以管理员命令去安装 E:\wamp ...
- (一)Linux相关内容的简介
1.Linux内核官网:www.kernel.org 2.Linux主要发行版本: (1)redhat 服务器中常见(部分功能收费); (2)Ubuntu 图形界面较好,但是服务器中很少用: (3 ...
- shell条件测试test
shell条件测试可以通过以下两种方式: test 参数 测试内容 [ 参数 测试内容 ] 一.测试文件类型: test -e 文件名 (测试文件是否存在) [ - ...
- 基于AngularJS的企业软件前端架构[转载]
这篇是我参加QCon北京2014的演讲内容: 提纲: 企业应用在软件行业中占有很大的比重,而这类软件多数现在也都采用B/S的模式开发,在这个日新月异的时代,它们的前端开发技术找到了什么改进点呢? B/ ...
- 30分钟全面解析-SQL事务+隔离级别+阻塞+死锁
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...