本文涉及到的概念
1.为什么重载equals方法时,要遵守通用约定
2.重载equals方法时,要遵守哪些通用约定
 
为什么重载equals方法时,要遵守通用约定
Object类的非final方法都有明确的通用约定,这些方法是被设计成被重载的。重载时,如果不遵守通用约定,那么,其它依赖于这些通用约定的类(例如HashMap和HashSet)就无法结合该类一起正常工作----<<effective java>>
 
 
quals方法实现了等价关系,重载时要遵守的通用约定:
a.自反性(reflexive)  对于任何非null的引用值x, x.equals(x)必须返回true。
b.对称性(symmetric)  对于任何非null的引用值x和y,当且仅当y.equals(x)返回true时,x.equals(y)必须返回true
c.传递性(transitive)   对于任何非null的引用值x,y和z,如果x.equals(y)返回true,并且y.equals(z)返回true,那么x.equals(z)返回true
d.一致性      对于任何非null的引用值x和y,只要equals的比较操作在对象中所用的信息没有被修改,多次调用x.equals(y)就会一致地返回

true,或者一致地返回false
e.对于任何非null的引用值x,x.equals(null)必须返回false
 
a.自反性
基本上不会违背这一条规定。如果违背了的话,将一个引用添加到一个集合中,然后,调用集合的contains(x)方法,它会返回false。x.equals(x)不等于true,导致contains(x)方法返回false。
 
b.对称性
对于任何非null的引用值x和y, x.equals(y)返回true, y.equals(x)也要返回true
public final class CaseInsensitiveString {
private final String s; public CaseInsensitiveString(String s) {
if (s == null)
throw new NullPointerException();
this.s = s;
} // Broken - violates symmetry!
@Override
public boolean equals(Object o) {
if (o instanceof CaseInsensitiveString)
return s.equalsIgnoreCase(((CaseInsensitiveString) o).s);
if (o instanceof String) // One-way interoperability!
return s.equalsIgnoreCase((String) o);
return false;
} // This version is correct.
// @Override public boolean equals(Object o) {
// return o instanceof CaseInsensitiveString &&
// ((CaseInsensitiveString) o).s.equalsIgnoreCase(s);
// } public static void main(String[] args) {
CaseInsensitiveString cis = new CaseInsensitiveString("Polish");
String s = "polish";
System.out.println(cis.equals(s) + " " + s.equals(cis));
}
}
可以把上述的例子代码,代入对称性公式,CaseInsensitivesString为x, String为y, CaseInsensitivesString为y.
x.equals(y),y.equals(x)都为true,当y是CaseInsensitivesString类型时;当y为String类型时,y.equals(x),就为false。
 
String类的equals方法的实现: stringInstance.equals(CanseInsensitivesStringIns),它会返回false,因为x不是String类型
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
c.传递性
equals预定的第三个要求是,如果一个对象等于第二个对象,并且第二个对象又等于第三个对象,则第一个对象一定等于第三个对象。
----<<effective java>>
重写类的equals方法后,使用x.equals(y), y.equals(z),x.equals(z),去检验。如果发现不符合,则违反了该通用约定,那么,就要重新实现。
 
下面的例子,违反了传递性:
父类Point,Point类之间的比较,重写equals方法,比较内容是否相等,不是比较引用地址是否相等。
public class Point {
private final int x;
private final int y; public Point(int x, int y) {
this.x = x;
this.y = y;
} @Override
public boolean equals(Object o) {
if (!(o instanceof Point))
return false;
Point p = (Point) o;
return p.x == x && p.y == y;
} // Broken - violates Liskov substitution principle - Pages 39-40
// @Override public boolean equals(Object o) {
// if (o == null || o.getClass() != getClass())
// return false;
// Point p = (Point) o;
// return p.x == x && p.y == y;
// } // See Item 9
@Override
public int hashCode() {
return 31 * x + y;
}
}

创建一个子类,继承Point类

import java.awt.Color;

public class ColorPoint extends Point {
private final Color color; public ColorPoint(int x, int y, Color color) {
super(x, y);
this.color = color;
} // Broken - violates symmetry!
//x为Point,y为ColorPoint
// x.equals(y)为true,但是y.equals(x)为false
@Override
public boolean equals(Object o) {
if (!(o instanceof ColorPoint))
return false;
return super.equals(o) && ((ColorPoint) o).color == color;
} // Broken - violates transitivity!
// @Override public boolean equals(Object o) {
// if (!(o instanceof Point))
// return false;
//
// // If o is a normal Point, do a color-blind comparison
// if (!(o instanceof ColorPoint))
// return o.equals(this);
//
// // o is a ColorPoint; do a full comparison
// return super.equals(o) && ((ColorPoint)o).color == color;
// }
//p1.euqlas(p2),p2.equals(p3),p1.equals(p3)
//输出结果:输出结果 true, true ,false public static void main(String[] args) {
// First equals function violates symmetry
Point p = new Point(1, 2);
ColorPoint cp = new ColorPoint(1, 2, Color.RED);
System.out.println(p.equals(cp) + " " + cp.equals(p)); // Second equals function violates transitivity
ColorPoint p1 = new ColorPoint(1, 2, Color.RED);
Point p2 = new Point(1, 2);
ColorPoint p3 = new ColorPoint(1, 2, Color.BLUE);
System.out.printf("%s %s %s%n", p1.equals(p2), p2.equals(p3), p1.equals(p3));
}
}
p1和p2的比较,不考虑颜色;p2和p3的比较,也不考虑颜色;p1和p3的比较考虑颜色,于是p1和p3不相等。违反了传递性原则,x.equals(y),y.equals(z).x.equals(z)
 
如何解决这个问题?
事实上,这是面向对象语言中关于等价关系的一个基本问题。我们无法在扩展可实例化的类的同时,既增加新的值组件,同时又保留equals约定,除非愿意放弃面向对象的抽象所带来的优势。
也就是,我们无法在扩展父类,然后,同时在子类中保留父类和子类的equals约定。
 
解决办法:
a.复合优先于继承
b.父类是抽象类(没有任何值组件),子类添加值域
 
使用复合的方式来解决

// Simple immutable two-dimensional integer point class - Page 37

import java.util.*;

public class Point {
private final int x;
private final int y; public Point(int x, int y) {
this.x = x;
this.y = y;
} @Override public boolean equals(Object o) {
if (!(o instanceof Point))
return false;
Point p = (Point)o;
return p.x == x && p.y == y;
} // See Item 9
@Override public int hashCode() {
return 31 * x + y;
}
}
public enum Color { RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET } // Adds a value component without violating the equals contract - Page 40
public class ColorPoint {
private final Point point;
private final Color color; public ColorPoint(int x, int y, Color color) {
if (color == null)
throw new NullPointerException();
point = new Point(x, y);
this.color = color;
} /**
* Returns the point-view of this color point.
*/
public Point asPoint() {
return point;
} @Override public boolean equals(Object o) {
if (!(o instanceof ColorPoint))
return false;
ColorPoint cp = (ColorPoint) o;
return cp.point.equals(point) && cp.color.equals(color);
} @Override public int hashCode() {
return point.hashCode() * 33 + color.hashCode();
}
} /////////////////////////////////////////////////////////////////////////
public class Test { public static void main(String[] args) {
// First equals function violates symmetry
Point p = new Point(1, 2);
ColorPoint cp = new ColorPoint(1, 2, Color.RED);
System.out.println(p.equals(cp) + " " + cp.equals(p)); // Second equals function violates transitivity
ColorPoint p1 = new ColorPoint(1, 2, Color.RED);
Point p2 = new Point(1, 2);
ColorPoint p3 = new ColorPoint(1, 2, Color.BLUE);
System.out.printf("%s %s %s%n", p1.equals(p2), p2.equals(p3), p1.equals(p3));
}
}
输出结果:
false false
false false false
 

重载equals方法时要遵守的通用约定--自反性,对称性,传递性,一致性,非空性的更多相关文章

  1. 第八条——覆盖equals方法时需遵守的通用约定

    1)自反性 对于任何非null的引用值x,x.equals(x)必须返回true.---这一点基本上不会有啥问题 2)对称性 对于任何非null的引用值x和y,当且仅当x.equals(y)为true ...

  2. 覆盖equals方法时请遵守通用约定

    覆盖equals方法时请遵守通用约定   覆盖equals方法看起来很简单,但是有许多覆盖方式会导致错误,并且后果很严重.最容易避免这种类问题的方法就是不覆盖equals方法,在这种情况下,类的每个实 ...

  3. Effective Java 第三版——10. 重写equals方法时遵守通用约定

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  4. Effective Java 第三版——11. 重写equals方法时同时也要重写hashcode方法

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  5. [改善Java代码]覆写equals方法时不要识别不出自己

    建议45: 覆写equals方法时不要识别不出自己 我们在写一个JavaBean时,经常会覆写equals方法,其目的是根据业务规则判断两个对象是否相等,比如我们写一个Person类,然后根据姓名判断 ...

  6. 使用equals方法时,要注意

    这是我在项目中犯的一个低级错误: 使用equals方法时,要注意这个方法是boolean java.lang.String.equals(Object anObject)传递的是Object,所以传任 ...

  7. 第九条:覆盖equals方法时总要覆盖hashCode方法

    Object类的hashCode方法: public native int hashCode();   是一个本地方法. 其中这个方法的主要注释如下: Whenever it is invoked o ...

  8. 【Java实战】源码解析为什么覆盖equals方法时总要覆盖hashCode方法

    1.背景知识 本文代码基于jdk1.8分析,<Java编程思想>中有如下描述: 另外再看下Object.java对hashCode()方法的说明: /** * Returns a hash ...

  9. 为什么重写equals方法时,要求必须重写hashCode方法?

    1 equals方法 Object类中默认的实现方式是  :   return this == obj  .那就是说,只有this 和 obj引用同一个对象,才会返回true. 而我们往往需要用equ ...

随机推荐

  1. Hibernate(十四):HQL查询(三)

    背景 基于上两章节<Hibernate(十二):HQL查询(一)>.<Hibernate(十三):HQL查询(二)>,已经学习了一部分关于HQL的用法: HQL带参数查询 HQ ...

  2. CentOS 6.8下二级域名及目录的绑定

    二级域名对应目录的绑定: 第一步: 开启mod_rewrite模块,默认是开启的,这里可以查下是否开启 终端输入:vim /etc/httpd/conf/httpd.conf  回车 查看188行:L ...

  3. java中String类学习笔记

    1.String的两种实例化方式 String str="hello";//直接赋值的方式: String str=new String("hello");// ...

  4. 031718-js变量、数据类型、运算符

    1.关键字.标识符.变量(是一个名称,最好用字母开头,对大小写敏感).常量 (是有数据类型的一个值) 变量: ①定义并赋值 ②使用   2.数据类型:数字  字符串  布尔  null  undefi ...

  5. 在容器中运行 Jenkins pipeline 任务

    持续集成中的 pipeline 技术和 docker 都是当前正在发展的主流方向,当然把它们结合起来在 CI/CD 过程中发挥出更强大的威力也是大家共同的目标.本文将介绍如何在 Jenkins pip ...

  6. [LeetCode] Design Search Autocomplete System 设计搜索自动补全系统

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

  7. C++11的value category(值类别)以及move semantics(移动语义)

    转载请保留以下声明 作者:赵宗晟 出处:http://www.cnblogs.com/zhao-zongsheng/p/value_categories_and_move_semantics.html ...

  8. 【Matplotlib-01】Python 绘图库 Matplotlib 入门教程

    环境: Windows10 python3.6.4 numpy1.14.1 matplotlib2.1.2 工具:Cmder 目录: 1.线性图 2.散点图 3.饼状图 4.条形图 5.直方图 例1: ...

  9. [AH/HNOI2017]大佬

    题目描述 人们总是难免会碰到大佬.他们趾高气昂地谈论凡人不能理解的算法和数据结构,走到任何一个地方,大佬的气场就能让周围的人吓得瑟瑟发抖,不敢言语. 你作为一个 OIER,面对这样的事情非常不开心,于 ...

  10. VK Cup 2017 - Round 2

    FallDream打的AB都FFT了,只剩一个我打的C,没进前一百,之后看看马拉松复活赛有没机会呗. A. Voltage Keepsake 题目大意:n个东西,每个东西一开始有bi能源,每秒消耗ai ...