如何重写equals方法呢?
我们先看下Object类中式如何定义equals方法的:
public boolean equals(Object obj) {
return (this == obj);
}
该方法指示其他某个对象是否与此对象“相等”。 通常情况下,我们只需要比较两个对象的内容是否相等直接使用equals方法即可,而当关注它们的内存地址是相等时才相等,我们就要重写equals方法,注意:当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。
下面我们来举个例子:
相关代码一:
public class Point {
private int x;
private int y;
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public static void main(String[] args) {
Point p1 = new Point(3, 3);
Point p2 = new Point(5, 5);
Point p3 = new Point(3, 3);
Collection<Point> collection = new HashSet<Point>();
collection.add(p1);
collection.add(p2);
collection.add(p3);
System.out.println(collection.size());// 3
}
}
//相信大家对上面的代码运行结果没有任何异议吧,一个HashSet的对象中存储有三个不同的对象。
在实际应用中,我们可能只希望p1和p3“相等”,只保存其中一个,这是就要重写equals和hashcode方法,
我们用eclipse的快捷方式生成这两个方法,代码如下:
相关代码二:
public class Point {
private int x;
private int y;
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public static void main(String[] args) {
Point p1 = new Point(3, 3);
Point p2 = new Point(5, 5);
Point p3 = new Point(3, 3);
Collection<Point> collection = new HashSet<Point>();
collection.add(p1);
collection.add(p2);
collection.add(p3);
System.out.println(collection.size());// 2
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
//用eclipse的快捷方式重写equals和hashcode方法后,就会把p3和p1看成“相等”的,于是p3就不会再存储在HashSet中。也很容易理解吧!
相关代码三:
public class Point {
private int x;
private int y;
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public static void main(String[] args) {
Point p1 = new Point(3, 3);
Point p2 = new Point(5, 5);
Point p3 = new Point(3, 3);
Collection<Point> collection = new HashSet<Point>();
collection.add(p1);
collection.add(p2);
collection.add(p3);
System.out.println(collection.size());// 3
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
public boolean equals(Point obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
//大家注意到此时的输出怎么会是3,我明明重写了 hashCode和equals方法,怎么p3还是被存储进去了呢,我们仔细看下,代码二和代码三,有啥不同,不同如下:
.equals方法上面少了@Override
.equals(Object obj)变成了equals(Point obj)
下面我们把@Override加到equals(Point obj)的上面,此时会出现编译错误:"The method equals(Point) of type Point must override a superclass method",意思就是说equals(Point)并不是重写的方法,怎么就不是重写呢?Object类的方法是equals(Object obj),此时的方法是equals(Point),看到了区别了吧,参数类型不同,当然就不算是重写,而是重载,所以我们可以通过在方法前面加上@Override来判断该方法是否被重写。所以我们在重写equal方法时,最好采用eclipse的快捷方式生成,不要想当然的把equal方法参数中的Object改成自己的类名,这样就大错特错了,不起到我们想要的效果!