根类Object中的equals方法描述:

public boolean equals(Object obj)
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returnstrue if and only if x and y refer to the same object (x == y has the value true).
String类重写了equals方法:
public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object
1.基本数据类型,也称原始数据类型。byte,short,char,int,long,float,double,boolean
  他们之间的比较,应用双等号(==),比较的是他们的值。
2.复合数据类型(类)
  当他们用(==)进行比较的时候,比较的是他们在内存中的存放地址,所以,除非是同一个new出来的对象,他们的比较后的结果为true,否则比较后结果为false。
JAVA当中所有的类都是继承于Object这个基类的,在Object中的基类中定义了一个equals的方法,这个方法的初始行为是比较对象的内存地址(即引用里的内容),但在一些类库当中这个方法被覆盖掉了,如String,Integer,Date在这些类当中equals有其自身的实现,而不再是比较类在堆内存中的存放地址了。
  对于复合数据类型之间进行equals比较,在没有覆写equals方法的情况下,他们之间的比较还是基于他们在内存中的存放位置的地址值的,因为Object的equals方法也是用双等号(==)进行比较的,所以比较后的结果跟双等号(==)的结果相同。
1.何时需要重写equals()
当一个类有自己特有的“逻辑相等”概念(不同于对象身份的概念)。
2.设计equals()
[1]使用instanceof操作符检查“实参是否为正确的类型”。
[2]对于类中的每一个“关键域”,检查实参中的域与当前对象中对应的域值。
[2.1]对于非float和double类型的原语类型域,使用==比较;
[2.2]对于对象引用域,递归调用equals方法;
[2.3]对于float域,使用Float.floatToIntBits(afloat)转换为int,再使用==比较;
[2.4]对于double域,使用Double.doubleToLongBits(adouble) 转换为int,再使用==比较;
[2.5]对于数组域,调用Arrays.equals方法。
public boolean equals(Object obj)
{
if(obj == null) return false; //keep the object compared not be null
else{
if(obj instanceof Cat) //keep the object input be Cat
{
Cat c = (Cat) obj; //Cast to Cat
if(c.color == this.color&& c.height == this.hright)
{
return true;
}
}
} return false;
}
3.当改写equals()的时候,总是要改写hashCode()
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
根据一个类的equals方法(改写后),两个截然不同的实例有可能在逻辑上是相等的,但是,根据Object.hashCode方法,它们仅仅是两个对象。因此,违反了“相等的对象必须具有相等的散列码”。
4.设计hashCode()
[1]把某个非零常数值,例如17,保存在int变量result中;
[2]对于对象中每一个关键域f(指equals方法中考虑的每一个域):
[2.1]boolean型,计算(f ? 0 : 1);
[2.2]byte,char,short型,计算(int);
[2.3]long型,计算(int) (f ^ (f>>>32));
[2.4]float型,计算Float.floatToIntBits(afloat);
[2.5]double型,计算Double.doubleToLongBits(adouble)得到一个long,再执行[2.3];
[2.6]对象引用,递归调用它的hashCode方法;
[2.7]数组域,对其中每个元素调用它的hashCode方法。
[3]将上面计算得到的散列码保存到int变量c,然后执行 result=37*result+c;
[4]返回result。
5.示例
下面的这个类遵循上面的设计原则,重写了类的equals()和hashCode()。
 package com.zj.unit;
import java.util.Arrays; public class Unit {
private short ashort;
private char achar;
private byte abyte;
private boolean abool;
private long along;
private float afloat;
private double adouble;
private Unit aObject;
private int[] ints;
private Unit[] units; public boolean equals(Object o) {
if (!(o instanceof Unit))
return false;
Unit unit = (Unit) o;
return unit.ashort == ashort
&& unit.achar == achar
&& unit.abyte == abyte
&& unit.abool == abool
&& unit.along == along
&& Float.floatToIntBits(unit.afloat) == Float
.floatToIntBits(afloat)
&& Double.doubleToLongBits(unit.adouble) == Double
.doubleToLongBits(adouble)
&& unit.aObject.equals(aObject)
&& equalsInts(unit.ints)
&& equalsUnits(unit.units);
} private boolean equalsInts(int[] aints) {
return Arrays.equals(ints, aints);
} private boolean equalsUnits(Unit[] aUnits) {
return Arrays.equals(units, aUnits);
} public int hashCode() {
int result = 17;
result = 37 * result + (int) ashort;
result = 37 * result + (int) achar;
result = 37 * result + (int) abyte;
result = 37 * result + (abool ? 0 : 1);
result = 37 * result + (int) (along ^ (along >>> 32));
result = 37 * result + Float.floatToIntBits(afloat);
long tolong = Double.doubleToLongBits(adouble);
result = 37 * result + (int) (tolong ^ (tolong >>> 32));
result = 37 * result + aObject.hashCode();
result = 37 * result + intsHashCode(ints);
result = 37 * result + unitsHashCode(units);
return result;
} private int intsHashCode(int[] aints) {
int result = 17;
for (int i = 0; i < aints.length; i++)
result = 37 * result + aints[i];
return result;
} private int unitsHashCode(Unit[] aUnits) {
int result = 17;
for (int i = 0; i < aUnits.length; i++)
result = 37 * result + aUnits[i].hashCode();
return result;
}
}
关键是如何计算hashcode值.
此外要注意如何在Java中避免equals方法的隐藏陷阱(http://coolshell.cn/articles/1051.html)
下面给出比较成熟的equals()代码:
 public boolean equals(Object obj)
{
if(this == obj)
{
return true;
}
if(obj != null && obj.getClass() == Person.class)
{
Person personObj = (Person) obj;
if(this.getldStr().equals(personObj.getldStr()))
{
return true;
}
} return false;
}

忠告:1、覆盖equals时总要覆盖hashcode; 2、Don’t try to be too clever不要试图让equals方法过于智能; 3、Don’t substitute another type for Object in the equals declaration.不要将equals声明中Object对象替换为其他对象

public boolean equals(MyClass o) {
...
}需使用Object o

附转载:http://www.importnew.com/8701.html 或 http://www.programcreek.com/2011/07/java-equals-and-hashcode-contract/


The Java super class java.lang.Object has two very important methods defined:


public boolean equals(Object obj)
public int hashCode()

They have been proved to be extremely important to understand, especially when user-defined objects are added to Maps. However, even advanced-level developers sometimes can’t figure out how they should be used properly. In this post, I will first show an example of a common mistake, and then explain how equals() and hashCode contract works.


1. A common mistake


Common mistake is shown in the example below.


import java.util.HashMap;
 
public class Apple {
private String color;
 
public Apple(String color) {
this.color = color;
}
 
public boolean equals(Object obj) {
if (!(obj instanceof Apple))
return false;
if (obj == this)
return true;
return this.color.equals(((Apple) obj).color);
}
 
public static void main(String[] args) {
Apple a1 = new Apple("green");
Apple a2 = new Apple("red");
 
//hashMap stores apple type and its quantity
HashMap<Apple, Integer> m = new HashMap<Apple, Integer>();
m.put(a1, 10);
m.put(a2, 20);
System.out.println(m.get(new Apple("green")));
}
}

In this example, a green apple object is stored successfully in a hashMap, but when the map is asked to retrieve this object, the apple object is not found. The program above prints null. However, we can be sure that the object is stored in the hashMap by inspecting in the debugger:



2. Problem caused by hashCode()


The problem is caused by the un-overridden method “hashCode()”. The contract between equals() and hasCode() is that:
1. If two objects are equal, then they must have the same hash code.
2. If two objects have the same hashcode, they may or may not be equal.


The idea behind a Map is to be able to find an object faster than a linear search. Using hashed keys to locate objects is a two-step process. Internally the Map stores objects as an array of arrays. The index for the first array is the hashcode() value of the key. This locates the second array which is searched linearly by using equals() to determine if the object is found.


The default implementation of hashCode() in Object class returns distinct integers for different objects. Therefore, in the example above, different objects(even with same type) have different hashCode.


Hash Code is like a sequence of garages for storage, different stuff can be stored in different garages. It is more efficient if you organize stuff to different place instead of the same garage. So it’s a good practice to equally distribute the hashCode value. (Not the main point here though)


The solution is to add hashCode method to the class. Here I just use the color string’s length for demonstration.


public int hashCode(){
return this.color.length();
}

java对象equals方法的重写的更多相关文章

  1. 关于重写对象equals方法的问题

    1.==和equals的区别 a.基本数据类型使用  == 进行值的比较 b.引用类型使用 == 进行比较时,直接比较的是对象的存储地址,即两个引用是否指向了同一个对象. c.equals方法是基类O ...

  2. java代码equals方法

    package com.bc; public class Test_6 { // 我们知道java中的每个类都继承自Object类,equals是Object方法之一 String name; int ...

  3. java基础—equals方法

    一.equals方法介绍 1.1.通过下面的例子掌握equals的用法 1 package cn.galc.test; 2 3 public class TestEquals { 4 public s ...

  4. Java的equals方法的使用技巧

    Java的equals方法的使用技巧 1.业务场景: 在某个社交软件中,要求每个用户的用户名(name)必须独一无二,那么在每次增加新用户的时候,都要对该用户的注册名进行判断,如果当前用户名已经被占用 ...

  5. 为什么重写 equals 方法 必须重写 hashCode

    自己学到这,就记录了下来,代码都是自己敲得,有不对的地方希望大神指点出来 为什么重写 equals 方法 必须重写 hashCode 如果你重写了equals,比如说是基于对象的内容实现的,而不重写 ...

  6. JAVA中equals方法与hashCode方法学习

    首先参考文章:http://www.oschina.net/translate/working-with-hashcode-and-equals-methods-in-java 1,equals方法的 ...

  7. Java==与equals方法的区别

    摘自:http://www.cnblogs.com/dolphin0520/p/3592500.html 1.对于==,如果作用于基本数据类型的变量,则直接比较其存储的 “值”是否相等: 如果作用于引 ...

  8. java中equals方法和==的用法

    java中equals方法的用法以及==的用法(参考一)equals 方法是 java.lang.Object 类的方法.两种用法说明:(1对于字符串变量来说,使用“==”和“equals()”方法比 ...

  9. Java中“==”与equals的区别以及equals方法的重写

    一.“==”与equals的区别: (1)==代表比较双方是否相同: 基本数据类型表示值相等. 引用数据类型表示地址相等,即同一个对象. (2)Object中的equals()方法:是否为同一个对象的 ...

随机推荐

  1. sql语句感想

    select出来内容可以当成表拿来用,,比如取别名什么的. union是纵向的,追加记录(行) join on是横向的,追加列

  2. ps 简介

    1. ps 简介ps 命令就是最根本相应情况下也是相当强大地进程查看命令.运用该命令可以确定有哪些进程正在运行和运行地状态. 进程 是否结束.进程有没有僵死.哪些进程占用了过多地资源等等.总之大部分信 ...

  3. ligerui+json_002_Grid用法、属性总结

    原文更全面,地址: http://blog.csdn.net/dxnn520/article/details/8216560 // ================================== ...

  4. java 控制器向页面传值方式

    1.单个字符串 @RequestMapping(value = "list", method = RequestMethod.GET) public ModelAndView li ...

  5. Oracle存储过程总结

    1.存储过程结构 1.1 第一个存储过程 create or replace procedure proc1( para1 varchar2, para2 out varchar2, para3 in ...

  6. android怎么换背景图片

    我不晓得一般是怎么做的,但是至少可以用两种方法,一种是用一个全屏的ImageView来当作背景,通过修改imageview来修改背景图片,一种是将你xml中最外层的那个布局,LinerLayout之类 ...

  7. CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页。

    CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/    此为注释语法 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控 ...

  8. 【crunch bang】安装firefox,删除iceweasel

    首先,移除iceweasel: apt-get remove iceweasel Then, download the latest Linux build of Firefox directly f ...

  9. 衣明志是个SB

    面试碰到衣明志,问了些傻逼问题,尼玛就是一不折不扣的蠢驴. 这个人太能装了,而且水平也不咋地.

  10. Windows系统文件受损的修复技巧

    Windows2000/XP系统文件受损的修复技巧 意外重启.安装了不兼容的软件.恶意程序侵扰.误删文件……有太多种可能性会使我们的系统文件受损,而系统文件受损后最直接的表现就是系统不稳定.经常出现错 ...