原文地址: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 IdentityHashMap in 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.

转载于:https://www.cnblogs.com/davidwang456/p/6026280.html

Difference Between HashMap and IdentityHashMap--转相关推荐

  1. JAVA day20、21 双列集合Map<K,V>:HashMap,LinkedHashMap,TreeMap,Hashtable, ConcurrentHashMap;JDK1.9新特性

    一.Map<K,V> Java提供了专⻔的集合类⽤来存放这种这种⼀⼀对应的关系,叫做映射对象,即 java.util.Map 接⼝. 类型参数: K - 此映射所维护的键的类型 V - 映 ...

  2. Java集合之HashMap

    在总集篇中我大概梳理了一下整个集合类的关系,这篇文章是对总集篇的扩展,本文将详细讨论HashMap的实现原理.所有涉及到的源码都是基于JDK11.顺便插一句,大家要学就学新的嘛,毕竟11可是长期支持版 ...

  3. Java 集合类(一)

    今天我们先讲一下Collection: Collection和Collections的区别: java.util.Collection是一种java集合接口,它提供了对集合对象的基本操作通用接口方法, ...

  4. 多线程面试题_100多线程和Java并发面试问答–最终清单(PDF下载)

    多线程面试题 在这篇文章中,我们将提供有关多线程和Java并发面试问答的综合文章. 编者注:并发始终是开发人员的挑战,编写并发程序可能非常困难. 引入并发时,有很多事情可能会崩溃,并且系统的复杂性会大 ...

  5. java集合框架(Framework)的性能

    关于Java集合框架里面常用类的性能测试比较,包括(ArrayList/LinkedList /Vector/Queue/TreeSet/HashSet/LinkedHashSet/TreeMap/H ...

  6. java-数据结构-续

    接口: Collection<E> 子接口: BlockingDeque<E>, BlockingQueue<E>, Deque<E>, List< ...

  7. java中的数据结构总结

    Java的类库实在是很多,以至于很多人都不太了解,结果总是自己造轮子. 下面汇总了Java中的一些数据结构,加上一些实现的分析,同时备忘. 至于时间复杂度,个人觉得写出来的用处不大.如果明白它是怎么实 ...

  8. java安全编码指南之:Mutability可变性

    文章目录 简介 可变对象和不可变对象 创建mutable对象的拷贝 为mutable类创建copy方法 不要相信equals 不要直接暴露可修改的属性 public static fields应该被置 ...

  9. java 容器_java容器考点总结和源码剖析!!!

    出自:https://github.com/CyC2018/CS-Notes 一.概览 Collection Map 二.容器中的设计模式 迭代器模式 适配器模式 三.源码分析 ArrayList V ...

最新文章

  1. pandas 实现DataFrame的合并Merge
  2. 中国计算机专业专科学校排名2015,计算机专科学校排名前十(最出名的10所计算机学院)...
  3. 电脑服务器不稳定怎么办,网速不稳定怎么办? 网速不稳定的原因与解决办法-电脑教程...
  4. OpenCV之imgproc 模块. 图像处理(3)霍夫线变换 霍夫圆变换 Remapping 重映射 仿射变换
  5. 金融贷款逾期的模型构建6——特征选择
  6. jquery实现截取pc图片_jquery实现图片裁剪思路及实现
  7. 微服务组件记事本:Skywalking的ES索引 · 收藏篇
  8. 40:数组中只出现一次的数字
  9. java 防止表单重复提交
  10. Redis设计与实现阅读总结(一)数据结构和对象
  11. 一个成功的BI项目实施需要注意哪些?
  12. python自学网站-自学Python网站推荐 从入门到精通
  13. Java试题库(含答案)
  14. 通达信公式编写使用哪种计算机语言,通达信公式编写入门(附通达信经典实用选股公式).pdf...
  15. 嵌入式入门必去的网站 —— 介绍的非常详细
  16. 社交网络用户行为分析,各类社交软件用户分析
  17. ubuntu系统安装完nvidia显卡驱动后黑屏,不能进入系统
  18. BME相关SCI期刊
  19. 从初学者的角度看Solr概述
  20. jquery保留两位小数方法,字符串保留两位小数

热门文章

  1. mysql 从数据库配置文件_mysql数据库配置文件
  2. python程序设计与科学计算pdf_用Python做科学计算 pdf版
  3. vscode 注释_VSCode最强助攻
  4. html标记表示超链接,HTML常见标签学习
  5. 2009年计算机二级c语言考试真题,2009年3月全国计算机二级C语言考试真题.doc
  6. 单片机实现环形队列_单片机模块化程序: 难道有环形队列串口发送数据就万事大吉了吗...
  7. linux 搭建开发stm32 stlink,Ubuntu下搭建stm32+stlink的开发环境
  8. shell连接工具_无需本地软件,只用浏览器就能连接并控制安卓手机
  9. java 提供的排序方法_请给出java几种排序方法
  10. Android:安卓APP开发显示一个美女,安卓APP开发显示两个美女