1、背景 

 今天翻开IdentityHashMap的时候,就傻眼了,这个到底是个逻辑啊,我的程序代码如下:

  IdentityHashMap<String,String> identityHashMap=new IdentityHashMap<String, String>();

     private void initMap(){
identityHashMap.put("zhangsan","age:18");
identityHashMap.put("lisi","age:24");
} public String getAge(String name){
return identityHashMap.get(name);
} public static void main(String[] args) {
IdentityHashMapTest test=new IdentityHashMapTest();
test.initMap(); String zhangsan=new String("zhangsan");
String lisi=new String("lisi");
System.out.println("zhangsan age is ="+ test.getAge(zhangsan));
System.out.println("lisi age is ="+ test.getAge(lisi)); }

运行的结果如下:

zhangsan age is =null
lisi age is =null

为什么如此呢?

2、源码探索

This class implements the <tt>Map</tt> interface with a hash table, using
* reference-equality in place of object-equality when comparing keys (and
* values). In other words, in an <tt>IdentityHashMap</tt>, two keys
* <tt>k1</tt> and <tt>k2</tt> are considered equal if and only if
* <tt>(k1==k2)</tt>. (In normal <tt>Map</tt> implementations (like
* <tt>HashMap</tt>) two keys <tt>k1</tt> and <tt>k2</tt> are considered equal
* if and only if <tt>(k1==null ? k2==null : k1.equals(k2))</tt>.)
也就是说IdentityHashMap的用法是,如果两个key是引用相等的时候,才会返回存在map里面的数据,否则返回为null,并不是像HashMap中的只是比较key的值是否相等
(k = p.key) == key || (key != null && key.equals(k))

put方法:

 public V put(K key, V value) {
Object k = maskNull(key);
Object[] tab = table;
int len = tab.length;
int i = hash(k, len); Object item;
while ( (item = tab[i]) != null) {
if (item == k) {
V oldValue = (V) tab[i + 1];
tab[i + 1] = value;
return oldValue;
}
i = nextKeyIndex(i, len);
} modCount++;
tab[i] = k;
tab[i + 1] = value;
if (++size >= threshold)
resize(len); // len == 2 * current capacity.
return null;
}

  从这里可以看出,int i = hash(k, len);一定是一个偶数值,并且这里使用的while循环操作,可能会有性能问题。

  这里key是放在偶数的位置,而value是存放在key对应的下个一个位置中  

  当size>threshold的时候,就会去扩容。

get方法

 public V get(Object key) {
Object k = maskNull(key);
Object[] tab = table;
int len = tab.length;
int i = hash(k, len);
while (true) {
Object item = tab[i];
if (item == k)
return (V) tab[i + 1];
if (item == null)
return null;
i = nextKeyIndex(i, len);
}
}

  这里查找value的时候也是使用while循环查找,不断的去轮询找key,知道遇到空。

  也是使用强引用的比较,如果是key1==key2,才会返回对应的值。

3、使用场景

  现在想想这中map的设计,也挺有意思的。

  比如我想在户口登记处中找到一个叫张三的,他现在几岁了?全国叫张三的人这么多,户口注册的时候,又没有说不能重名,我怎么知道这个张三是否是你要找的那个张三呢?所以这个时候就可以使用IdentityHashMap来存储了,只有是保证强引用的时候,才可以找到,否则,我还是会告诉你,我不知道张三是谁

java-map-IdentityHashMap的更多相关文章

  1. java:Map借口及其子类HashMap五,identityHashMap子类

    java:Map借口及其子类HashMap五,identityHashMap子类 了解:identityHashMap子类 一般情况下,标准的Map,是不会有重复的key值得value的,相同的key ...

  2. 入门:Java Map<String,String>遍历及修改

    重点:在使用Map时注意key-value,key用于检索value的内容. 在正常情况下,可以不允许重复:在java中分为2中情况,一是内存地址重复,另一个是不同的地址但内容相等. 在使用Map是一 ...

  3. Java Map操作

    Map:键必须是唯一 同步方法:Map m = Collections.synchronizedMap(new TreeMap(...)); Hashtable:基于散列表的实现 允许空键空值 线程安 ...

  4. Java map 详解 - 用法、遍历、排序、常用API等

    尊重原创: http://www.cnblogs.com/lzq198754/p/5780165.html 概要: java.util 中的集合类包含 Java 中某些最常用的类.最常用的集合类是 L ...

  5. java Map实现的cache manager

    一个模仿memcached的JAVA虚拟缓存工具,可以缓存java对象 import java.io.ByteArrayInputStream; import java.io.ByteArrayOut ...

  6. java map遍历、排序,根据value获取key

    Map 四种遍历: Map<String,String> map = new HashMap<String, String>(); map.put("one" ...

  7. java Map常用方法封装

      java Map常用方法封装 CreationTime--2018年7月16日15点59分 Author:Marydon 1.准备工作 import java.util.HashMap; impo ...

  8. Java Map获取key和value 以及String字符串转List方法

    一.问题描述 这里描述两个问题: 1.Java Map获取key和value的方法: 2.String字符串转List的方法: 二.解决方法 1.Java Map获取key和value的方法   2. ...

  9. java map添加另一个map时候 键值对的类型要一致

    java map添加另一个map时候 键值对的类型要一致

  10. java:Map借口及其子类HashMap四

    java:Map借口及其子类HashMap四 使用非系统对象作为key,使用匿名对象获取数据 在Map中可以使用匿名对象找到一个key对应的value. person: public class Ha ...

随机推荐

  1. 【jQuery】jQuery API 过 一 遍

    closest, parents <!DOCTYPE html> <html> <head> <meta charset="utf-8"& ...

  2. HBase HTablePool

    Instead of creating an HTable instance for every request from your client application, it makes much ...

  3. 【日语】secret base

    君(きみ)と夏(なつ)の终(お)わり 将来(しょうらい)の梦(ゆめ)Kimi to natsu no owari shourai no yume大(おお)きな希望(きぼう) 忘(わす)れないOoki ...

  4. 谈谈托管代码、IL、CLR、ISAPI?

    什么是托管代码?       托管代码是可以使用20多种支持Microsoft .NET Framework的高级语言编写的代码,这些语言包括:C#, J#, Microsoft Visual Bas ...

  5. dos 下 注册win 组件 ocx 后缀的

    C:\WINDOWS\system32>regsvr32 NTGraph.ocx

  6. C#日历上显示节气、阴阳历节假日信息

    近期在做一个工作日历,想在日历上设置工作日,显示请假.加班等相关信息,显示农历日期信息(包括农历日期.节日 .节气).公历信息(节假期). 不能不说,http://www.cnblogs.com/ho ...

  7. Linux下Django的安装

    1.下载Django.地址:https://www.djangoproject.com/download/ 2.解压3中得到的Django-1.6.2.tar.gz.使用下面的命令进行解压,解压后在当 ...

  8. jquery width(), innerWidth(), outerWidth() 区别

    #div1 { width: 100px; height: 100px; border: 5px black solid; padding: 10px; margin: 10px; backgroun ...

  9. Transact-SQL 数据类型转换

    Syntax   Syntax for CAST: CAST ( expression AS data_type [ ( length ) ] )     Syntax for CONVERT: CO ...

  10. WASD控制UI界面血条加减

    using UnityEngine; using System.Collections; using UnityEngine.UI; public class HealthController : M ...