java中Long和Integer在hashmap判断key是否存在的问题
来源:互联网 发布:淘宝卖家怎么设置折扣 编辑:程序博客网 时间:2024/06/07 01:55
1、HashMap中是否存在key的判断方法如下,归根结底是判断key的hashcode方法:
public boolean containsKey(Object key) { return getEntry(key) != null;}
final Entry<K,V> getEntry(Object key) { if (size == 0) { return null; } int hash = (key == null) ? 0 : hash(key); for (Entry<K,V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } return null;}
final int hash(Object k) { int h = hashSeed; if (0 != h && k instanceof String) { return sun.misc.Hashing.stringHash32((String) k); } h ^= k.hashCode(); // This function ensures that hashCodes that differ only by // constant multiples at each bit position have a bounded // number of collisions (approximately 8 at default load factor). h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4);}
2、而Long和Integer的hashcode方法分别如下:
Long:
public int hashCode() { return (int)(value ^ (value >>> 32));}
Integer:
public int hashCode() { return value;}
3、问题来了,
当hashmap的key为Long时,用数值a相等的Integer去判断a这个数值可以是否在hashmap中(containsKey),
结果是false;
4、解决方法:
4-1:将Integer强制转换成Long。
4-2:思考中。。。
0 0
- java中Long和Integer在hashmap判断key是否存在的问题
- java 多种判断key是否在map中存在的方法
- JAVA判断数据表是否在数据库中存在的方法
- Java HashMap使用String,Long,Integer作为key的性能测试
- 判断python字典中key是否存在的
- 判断python字典中key是否存在的
- java 判断json 某key是否存在
- 在java中如何判断一个类是否存在
- 判断json是否存在KEY
- java 中Long和Integer不得不知道的秘密
- php中判断memcache中key,value是否存在
- [SQL,Java]判断某一个已知表名的表是否在数据库中存在的方法
- 在Visual foxpro 中判断文件是否存在的函数
- php 判断数组中key值是否存在
- JS判断json中某个key是否存在
- 判断map集合中是否存在某个key
- 关于如何判断jQuery中对象是否存在的问题
- java访问远程文件判断是否存在的问题
- 二分
- 牛逼的电商运营为什么不自己开店铺要去打工?(知乎精选)
- TensorLayer
- 在C++中与QML对象交互
- Spring常见面试题
- java中Long和Integer在hashmap判断key是否存在的问题
- Oracle NVL,NVL2,COALESCE函数学习
- L1-020. 帅到没朋友
- C# 事件(Event)
- 9.png图片的处理
- ThreadPoolExecutor原理
- 企鹅TV OTT
- ZOJ1221(uva567)
- 总结一下自己在Android开发中遇到的时间处理