java-map-IdentityHashMap
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的更多相关文章
- java:Map借口及其子类HashMap五,identityHashMap子类
java:Map借口及其子类HashMap五,identityHashMap子类 了解:identityHashMap子类 一般情况下,标准的Map,是不会有重复的key值得value的,相同的key ...
- 入门:Java Map<String,String>遍历及修改
重点:在使用Map时注意key-value,key用于检索value的内容. 在正常情况下,可以不允许重复:在java中分为2中情况,一是内存地址重复,另一个是不同的地址但内容相等. 在使用Map是一 ...
- Java Map操作
Map:键必须是唯一 同步方法:Map m = Collections.synchronizedMap(new TreeMap(...)); Hashtable:基于散列表的实现 允许空键空值 线程安 ...
- Java map 详解 - 用法、遍历、排序、常用API等
尊重原创: http://www.cnblogs.com/lzq198754/p/5780165.html 概要: java.util 中的集合类包含 Java 中某些最常用的类.最常用的集合类是 L ...
- java Map实现的cache manager
一个模仿memcached的JAVA虚拟缓存工具,可以缓存java对象 import java.io.ByteArrayInputStream; import java.io.ByteArrayOut ...
- java map遍历、排序,根据value获取key
Map 四种遍历: Map<String,String> map = new HashMap<String, String>(); map.put("one" ...
- java Map常用方法封装
java Map常用方法封装 CreationTime--2018年7月16日15点59分 Author:Marydon 1.准备工作 import java.util.HashMap; impo ...
- Java Map获取key和value 以及String字符串转List方法
一.问题描述 这里描述两个问题: 1.Java Map获取key和value的方法: 2.String字符串转List的方法: 二.解决方法 1.Java Map获取key和value的方法 2. ...
- java map添加另一个map时候 键值对的类型要一致
java map添加另一个map时候 键值对的类型要一致
- java:Map借口及其子类HashMap四
java:Map借口及其子类HashMap四 使用非系统对象作为key,使用匿名对象获取数据 在Map中可以使用匿名对象找到一个key对应的value. person: public class Ha ...
随机推荐
- -_-#微信内置JavaScript API WeixinJSBridge
微信相关的 js 操作:分享.网络.菜单 微信内置JsAPI之WeixinJSBridge微信WeixinJSBridge API续
- POJ 3268 Silver Cow Party ( Dijkstra )
题目大意: 有N个农场每个农场要有一头牛去参加一个聚会,连接每个农场有m条路, 聚会地点是X,并且路是单向的.要求的是所有牛赶到聚会地点并且回到自己原先的农场所需要的最短时间. 题目分析: 其实就是以 ...
- 【贪心】XMU 1061 Ckp的约会
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1061 题目大意: n个任务(n<=1000),给定名字和开始.结束时间,求最多能 ...
- C# 中dataTable中的数据批量增加至数据表中
/// <summary> /// 批量添加 /// </summary> /// <param name="dt"></param> ...
- cf590A Median Smoothing
A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 关于Cookie和Session【转载】
当你第一次访问一个网站的时候,网站服务器会在响应头内加上Set-Cookie:PHPSESSID=nj1tvkclp3jh83olcn3191sjq3(php服务器),或Set-Cookie JSES ...
- 《鸟哥Linux私房菜基础学习篇》命令索引
在学习的过程,由于很多命令平时都用不着,因此做这个索引方便需要时查找.这包括了前两部分.主要是按页码顺序. P118 date:显示日期与时间 cal:显示日历 bc:计算器 P121 [Tab]:命 ...
- IMPLEMENTED IN PYTHON +1 | CART生成树
Introduction: 分类与回归树(classification and regression tree, CART)模型由Breiman等人在1984年提出,CART同样由特征选择.树的生成及 ...
- 【转】非常完善的Log4net详细说明
转自:http://www.cnblogs.com/zhangchenliang/p/4546352.htmlhttp://www.cnblogs.com/zhangchenliang/p/45463 ...
- clang和gcc消除警告
1. clang命令,它的作用是用来消除特定区域的clang的编译警告,-Wgnu则是消除?:警告, 例: #pragma clang diagnostic push #pragma clang di ...