Java hashCode 和 equals()
1 Object中定义的hashCode()
public int hashCode() Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by
HashMap .
The general contract of
JDK中的帮助文档,hashcode的使用约定是:
As much as is reasonably practical, the hashCode method defined by class 不覆盖Object的hashCode(),那么就可以理解为返回内存地址。 |
2 equals()
public boolean equals(Object obj) Indicates whether some other object is "equal to" this one.
The
The 这里讲了equals和==的关系。 x==y的意思就是二者的地址相同,如果equals不覆盖,那么==和equals一样。如果equals覆盖了,通常==指地址相等,equals指的是内容相同。 如String。 String str = "hello"; String str2 = new String("hello"); 问二者什么关系?==?equals()? 详细解释见Java String Note that it is generally necessary to override the 这里讲了和hashCode的关系。其实很简单,hashCode是为hashmap用的。假如equals,必须hashCode相同,否则hashmap无法使用了。 在Object中,equals和hashcode都是和地址相关的. 如果重载equals,是的二者内容相同。如果不重载hashcode,那么二者的hashcode和地址相关而不是内容相关,那么hashcode为不同值。导致hashmap无法使用。所以必须把hashcode也改为相似的语义。 |
3 String的hashCode()和equals()定义
当入参anObject不是null,且类型为String实例,长度相同,value一模一样的时候才返回true
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
//hashcode = s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value; for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
Java hashCode 和 equals()的更多相关文章
- Java hashCode() 和 equals()的若干问题
原文:http://www.cnblogs.com/skywang12345/p/3324958.html 本章的内容主要解决下面几个问题: 1 equals() 的作用是什么? 2 equals() ...
- Java hashCode() 和 equals()的若干问题解答
本章的内容主要解决下面几个问题: 1 equals() 的作用是什么? 2 equals() 与 == 的区别是什么? 3 hashCode() 的作用是什么? 4 hashCode() 和 equa ...
- Java hashCode() 和 equals()的若干问题解答<转载自skywang12345>
第1部分 equals() 的作用equals()的作用是用来判断两个对象是否相等.equals()定义在JDK的Object类中.通过判断两个对象的地址是否相等(即,是否是同一个对象)来区分它们是否 ...
- JAVA - hashcode与equals作用、关系
Hashcode的作用 总的来说,Java中的集合(Collection)有两类,一类是List,再有一类是Set.前者集合内的元素是有序的,元素可以重复:后者元素无序,但元素不可重复. ...
- 高强度学习训练第十二天总结:Java hashCode和equals的关系
今天要收拾东西.草草的总结下.. 1.如果两个对象相等,则hashcode一定也是相同的 2.两个对象相等,对两个对象分别调用equals方法都返回true 3.两个对象有相同的hashcode值,它 ...
- Java hashCode与equals学习
1.关于Object类的equals方法的特点 a) 自反性: x.equals(x) 应该返回true b) 对称性: x.equals(y)为true,那么y.equals(x) 也为true c ...
- java hashCode()与equals()的作用
1.hashcode是用来查找的,如果你学过数据结构就应该知道,在查找和排序这一章有 例如内存中有这样的位置 0 1 2 3 4 5 6 7 而我有个类,这个类有个字段叫ID,我要把这个 ...
- Java提高篇——equals()与hashCode()方法详解
java.lang.Object类中有两个非常重要的方法: 1 2 public boolean equals(Object obj) public int hashCode() Object类是类继 ...
- Java基础系列-equals方法和hashCode方法
原创文章,转载请标注出处:<Java基础系列-equals方法和hashCode方法> 概述 equals方法和hashCode方法都是有Object类定义的. publi ...
随机推荐
- luoguP2266 爱的距离
题目:http://www.luogu.org/problem/show?pid=2266 题解:感觉题意不清,就去瞅题解了T_T 然后发现好水... 类似于MST,我们把边从小到大加进去就可以了. ...
- 别再说“我已经努力了”,你的“努力”一文不值!
有次,让一个研究生男收集一份资料,快下班了问结果,竟然毛也没有.见我要怒,他慷慨激昂地说:"我已经很努力找了,但真的查不到." 作为主管,"我已经努力"这话我不 ...
- POI做题记录:第二届POI
Trees Memory limit: 32 MB Trees occur very often in computer science. As opposed to trees in nature, ...
- Oracle RAC中的投票算法
RAC集群中有三台机器,A,B,C A,B,C都会有3票,假设这是A的心跳线出现问题,整个RAC集群就划分为两个paritition, 一个是只有A的partition,一个是B,C组成的partit ...
- oracle空表导不出来
在oracle 11g r2中,使用exp有时候会导不出空的表,原因是这些表没有分配空间,手工分配空间即可导出. ----查询当前用户下的所有空表: select table_name from us ...
- opencv学习笔记-图像叠加、混合
在图像处理中,目标区域定义为感兴趣区域ROI(region of Interest),这是后期图像处理的基础,在获取ROI后,进行一些列的处理.ROI区域在Opencv中就是Rect,先构建Rect, ...
- 【Android - 框架】之Dagger2+MVP的用法
MVP模式本身相比于MVC模式就已经把View层和Controller层从Activity中进行了分离,将Model层和View层用Presenter层隔开,实现了初步的解耦.如果再加入Dagger2 ...
- Tomcat配置多个端口号或多个应用
一.在Tomcat下配置一个应用服务(service)中,配置多个端口号. 即一个service配置多个端口,项目可以通过多个端口访问. 修改tomcat-home\conf下的server.xml, ...
- HDOJ 4696 Answers 乱搞
乱搞: rt.有1就能输出全部的数,否则仅仅能输出偶数 Answers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/1 ...
- Groovy新手教程
Groovy新手教程 kmyhy@126.com 2009-5-13 一.groovy是什么 简单地说,Groovy 是下一代的java语言,跟java一样,它也执行在 JVM 中. 作为跑在JVM ...