浅谈Java中的hashCode和HashMap
来源:互联网 发布:java int 0x 编辑:程序博客网 时间:2024/06/05 21:11
先来看下Object中对hashCode的官方文档定义:
/** * Returns a hash code value for the object. This method is * supported for the benefit of hash tables such as those provided by * {@link java.util.HashMap}. * <p> * The general contract of {@code hashCode} is: * <ul> * <li>Whenever it is invoked on the same object more than once during * an execution of a Java application, the {@code hashCode} method * must consistently return the same integer, provided no information * used in {@code equals} comparisons on the object is modified. * This integer need not remain consistent from one execution of an * application to another execution of the same application. * <li>If two objects are equal according to the {@code equals(Object)} * method, then calling the {@code hashCode} method on each of * the two objects must produce the same integer result. * <li>It is <em>not</em> required that if two objects are unequal * according to the {@link java.lang.Object#equals(java.lang.Object)} * method, then calling the {@code hashCode} method on each of the * two objects must produce distinct integer results. However, the * programmer should be aware that producing distinct integer results * for unequal objects may improve the performance of hash tables. * </ul> * <p> * As much as is reasonably practical, the hashCode method defined by * class {@code Object} does return distinct integers for distinct * objects. (This is typically implemented by converting the internal * address of the object into an integer, but this implementation * technique is not required by the * Java™ programming language.) */
文档中并没有给出hashCode()的实现方法,底层是C++实现的,详情可查看:
https://www.zhihu.com/question/29976202
我们只要知道每个对象都会有一个int的hashCode值,hashCode的存在主要是用于查找的快捷性,如Hashtable,HashMap等,hashCode是用来在散列存储结构中确定对象的存储地址的。不同的对象可能会生成相同的hashcode值。虽然不能根据hashcode值判断两个对象是否相等,但是可以直接根据hashcode 值判断两个对象不等,如果两个对象的hashcode值不等,则必定是两个不同的对象。如果要判断两个对象是否真正相等,必须通过equals方法。重写方法可参考http://www.cnblogs.com/szlbm/p/5806226.html
在java编程语言中,最基本的结构就是两种,一个是数组,另外一个是模拟指针(引用),所有的数据结构都可以用这两个基本结构来构造的。Hashmap实际上是一个数组和链表的结合体(在数据结构中,一般称之为“链表散列“)。我们向Hashmap中添加元素时,先通过该元素key的hash决定元素的在数组中的存放位置,然后通过equal方法决定元素在链表的存放位置,如下图:
当我们往hashmap中put元素的时候,先根据key的hash值得到这个元素在数组中的位置(即下标),然后就可以把这个元素放到对应的位置中了。如果这个元素所在的位子上已经存放有其他元素了,那么在同一个位子上的元素将以链表的形式存放,新加入的放在链头,最先加入的放在链尾。从hashmap中get元素时,首先计算key的hashcode,找到数组中对应位置的某一元素,然后通过key的equals方法在对应位置的链表中找到需要的元素。
好吧,我只是谈了风月(理解),具体的实现推荐查看源码和这篇博客:http://www.iteye.com/topic/539465
- 浅谈Java中的hashCode和HashMap
- java中的hashmap和hashcode
- 浅谈Java中的hashcode和equals方法
- 浅谈java中的hashcode
- HashMap中的equals和hashCode
- 浅谈Java中的hashcode方法
- 浅谈Java中的hashcode方法
- 浅谈Java中的hashcode方法
- 浅谈Java中的hashcode方法
- 浅谈Java中的hashcode方法
- 浅谈Java中的hashcode方法
- 浅谈Java中的hashcode方法
- 浅谈Java中的hashcode方法
- 浅谈Java中的hashcode方法
- 浅谈Java中的hashcode方法
- 浅谈Java中的hashcode方法
- 浅谈Java中的hashcode方法
- 浅谈Java中的hashcode方法
- call_user_func()详解
- nginx服务器的事件驱动模型
- vim youcompeteme 插件安装
- ThinkPHP 验证码错误
- 关于Java中常用的集合类总结
- 浅谈Java中的hashCode和HashMap
- MySQL中的复制全表到新表
- andorid TextView(3)-使用HTML标签
- IOS Swift3.1 打印视图生命周期
- 进程间通信---消息队列
- 手把手教你搭建NDK开发环境以及基于JNI开发的简单DEMO
- STM32用SPI方式控制OLED模块
- mysql分组取每组前几条记录(排序)
- iOS开发:设置App名称,设置App icon图标,设置App启动图