本文参考

本篇文章参考自《Effective Java》第三版第十条"Obey the general contract when overriding equals"

the conditions when each instance of the class is equal only to itself

  • Each instance of the class is inherently unique —— 类的每一个实例本就彼此不同,例如Thread类,每一个线程仅和自身相等
  • There is no need for the class to provide a "logical equality" test —— 该类不具备"逻辑相等"的特点,例如两个字符串的相等需要比较二者每一个字符是否相等,因为它具备"逻辑相等"的特点(这些类可以被称为"值类"),而某些类的设计,如Pattern类,则没有提供equals()方法的重载来判断两个正则表达式是否相等,即不需要考虑"逻辑相等的情况"
  • A superclass has already overridden equals, and the superclass behavior is appropriate for this class —— 父类的equals()方法同样适用于子类,所以子类不需要再进行重载,例如绝大多数的Set实现类都继承了AbstractSet的equals()方法,下面是AbstractSet的equals()方法源码,显然已适用于绝大多数的Set实现

public boolean equals(Object o) {
    if (o == this)
        return true;

    if (!(o instanceof Set))
        return false;
    Collection<?> c = (Collection<?>) o;
    if (c.size() != size())
        return false;
    try {
        return containsAll(c);
    } catch (ClassCastException unused) {
        return false;
    } catch (NullPointerException unused) {
        return false;
    }
}

  • The class is private or package-private, and you are certain that its equals method will never be invoked —— 这个类是私有的或是包级私有的,可以确保它的equals()方法不会被调用,若被调用,可以将equals()方法设计为返回异常

value classes

前文已经讲到了"值类"具备"逻辑相等"的特点,但是注意并不是所有的"值类"都需要重载equals()方法,例如在"用静态工厂方法代替构造器"中我们提到了Boolean类的静态构造方法valueOf(booean b),它返回两个固定的类实例new Boolean(true)和new Boolean(false),若能够确保每个值至多只存在一个实例,则不需要重载equals()方法

枚举类型是其中一个特例

One kind of value class that does not require the equals method to be overridden is a class that uses instance control to ensure that at most one object exists with each value

general contract

  • Reflexive: For any non-null reference value x, x.equals(x) must return true
  • Symmetric: For any non-null reference values x and y, x.equals(y) must return true if and only if y.equals(x) returns true
  • Transitive: For any non-null reference values x, y, z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) must return true
  • Consistent: For any non-null reference values x and y, multiple invocations of x.equals(y) must consistently return true or consistently return false, provided no information used in equals comparisons is modified
  • For any non-null reference value x, x.equals(null) must return false

上述分别是自反性、对称性、传递性、一致性和x.equals(null) == false

当我们重载equals()方法时,应该问自己三个问题:它是否是对称的、传递的、一致的

There is no way to extend an instantiable class and add a value component while preserving the equals contract

我们无法在扩展(继承)可实例化的类的同时,既增加新的值组件,同时又保留equals约定,这在Java的Timestamp类中也有体现,Timestamp类继承了java.util.Date类,下面是源码注释

The Timestamp.equals(Object) method never returns true when passed an object that isn't an instance of java.sql.Timestamp, because the nanos component of a date is unknown. As a result, the Timestamp.equals(Object) method is not symmetric with respect to the java.util.Date.equals(Object) method. Also, the hashCode method uses the underlying java.util.Date implementation and therefore does not include nanos in its computation.

原文还指出了一种错误的写法例子,首先是父类Point的声明

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;
  }

  @Override public int hashCode() {

    return 31 * x + y;
  }
}

其次是子类ColorPoint类的声明

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

  @Override public boolean equals(Object o) {

    if (!(o instanceof ColorPoint))

      return false;

    return super.equals(o) && ((ColorPoint) o).color == color;
  }
}

当ColorPoint类的实例调用equals()方法和Point类的实例进行比较时,总是返回false,显然违背了对称性

但是我们可以通过"单向关联",将原本的"父类"作为成员变量加入到原本的"子类"中,通过非继承的方式,来遵守equals()方法的约定

// Adds a value component without violating the equals contract
public class
ColorPoint {

  private final Point point;

  private final Color color;

  public ColorPoint(int x, int y, Color color) {

    point = new Point(x, y);

    this.color = Objects.requireNonNull(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 31 * point.hashCode() + color.hashCode();
  }
}

do not write an equals method that depends on unreliable resources

倘若equals()方法依赖于不可靠的资源,可能会违背"一致性"约定,因为在不同的场合下调用equals()方法时,可能会有不同的结果

例如Java中的URL类,在源码注释中我们可以看到equals方法被标注为不符合"一致性"约定

The defined behavior for equals is known to be inconsistent with virtual hosting in HTTP

因为URL类实例的比较涉及IP地址是否相等,而IP地址可能会在不同网络环境下发生变化,所以是"不可靠"资源,无法满足"一致性"约定

here's a recipe for a high-quality equals method

  • Use the == operator to check if the argument is a reference to this object
  • Use the instanceof operator to check if the argument has the correct type
  • Cast the argument to the correct type
  • For each "significant" field in the class, check if that field of the argument matches the corresponding field of this object
  • Always override hashCode when you override equals(item 11)

上述提到的AbstractSet类的equals()方法就是一个很好的例子

public boolean equals(Object o) {

  // Use the == operator to check if the argument is a reference to this object

  if (o == this)

    return true;

  // Use the instanceof operator to check if the argument has the correct type

  if (!(o instanceof Set))

    return false;

  // Cast the argument to the correct type

  Collection<?> c = (Collection<?>) o;

  // check if that field of the argument matches the corresponding field of this object

  if (c.size() != size())

    return false;

  try {

    return containsAll(c);
  } catch (ClassCastException unused) {

    return false;
  } catch (NullPointerException unused) {

    return false;
  }
}

特别的,对于浮点型数值的比较,Java提供了Float.compare()和Double.compare()方法来考虑Float.NaN、-0.0f和0.0f这样特殊的情况,而它们重载的equals()方法没有对这种情况进行考虑,下面是equals()方法的源码注释

If f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false.
If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true.

因此,在比较两个值类实例的大小前,还要注意它的特殊取值

Idea自动生成equals()方法

public class TestAutoEquals {

  private String username;

  private int age;

  private boolean male;

  private String password;
}

idea提供了不同的"自动化"实现方式

  • idea default

@Override
public boolean equals(Object o) {

  if (this == o) return true;

  if (o == null || getClass() != o.getClass()) return false;

  TestAutoEquals that = (TestAutoEquals) o;

  if (age != that.age) return false;

  if (male != that.male) return false;

  if (!username.equals(that.username)) return false;

  return password.equals(that.password);
}

用getClass代替instanceof运算符来判断是否是同一个类的实例,尽管这样做能够解决上述"There is no way to extend an instantiable class and add a value component while preserving the equals contract"的问题,但有时候不能够采用这种替换方案,例如继承自同一个接口的不同实现类之间的比较或是父类和子类之间的比较

  • Apache commons-lang / commons-lang3

@Override
public boolean equals(Object o) {

  if (this == o) return true;

  if (o == null || getClass() != o.getClass()) return false;

  TestAutoEquals that = (TestAutoEquals) o;

  return new EqualsBuilder()
         .append(age, that.age)
         .append(male, that.male)
         .append(username, that.username)
         .append(password, that.password)
         .isEquals();
}

由特殊的Builder模式实现对每个成员变量的相等判断

  • Google guava / Java 7+

@Override
public boolean equals(Object o) {

  if (this == o) return true;

  if (o == null || getClass() != o.getClass()) return false;

  TestAutoEquals that = (TestAutoEquals) o;

  return age == that.age &&

         male == that.male &&

         Objects.equal(username, that.username) &&

         Objects.equal(password, that.password);
}

此处的Objects可以来自java.util包,也可以来自com.google.common.base包

另外可以由Google开发的AutoValue框架自动生成equals()方法,并且可以结合builder构建者模式,将在下一篇文章中讲解

Effective Java —— 覆盖equals时遵守通用约定的更多相关文章

  1. 第8条:覆盖equals时遵守通用约定

    如果不需要覆盖equals方法,那么就无需担心覆盖equals方法导致的错误. 什么时候不需要覆盖equals方法? 1.类的每个实例本质上是唯一的. 例如对于Thread,Object提供的equa ...

  2. Effective Java —— 覆盖equals时总要覆盖hashCode

    本文参考 本篇文章参考自<Effective Java>第三版第十一条"Always override hashCode when you override equals&quo ...

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

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

  4. Item 8 覆盖equals时请遵守通用约定

    在覆盖equals方法的时候,你必须要遵守它的通用约定,不遵守,写出来的方法,会出现逻辑错误.下面是约定的内容:   equals方法实现了等价关系:   自反性.对于任何非null的引用值,x.eq ...

  5. 第8条:覆盖equals时请遵守通用约定

    第8条:覆盖equals时请遵守通用约定 引言:尽管Object是一个具体类,但是设计它主要是为了拓展.它所有的非final方法(equals.hashCode.toString.clone和fina ...

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

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

  7. Item 9 覆盖equals时总要覆盖hashCode

    为什么覆盖equals时,总要覆盖hashCode?   原因是,根据Object规范: 如果两个对象根据equals(Object)方法比较是相等的,那么调用这两个对象中任意一个对象的hashCod ...

  8. 《Effective Java》笔记45-56:通用程序设计

    将局部变量的作用域最小化,可以增强代码的可读性和可维护性,并降低出错的可能性. 要使用局部变量的作用域最小化,最有力的方法就是在第一次使用它的地方才声明,不要过早的声明. 局部变量的作用域从它被声明的 ...

  9. 第八条:覆盖equals时请遵守通用约定

    ==是物理相等 equals是逻辑相等 因为每个类的实例对象本质上都是唯一的 ,利用物理相等(==)是指一个实例只能相等于它自己. 利用逻辑相等是(equals)指 一个实例是否和另一个实例的某些关键 ...

随机推荐

  1. (转)oracle 数据库性能健康检查脚本

    转至:https://blog.csdn.net/cm_0205/article/details/100210526?utm_medium=distribute.pc_relevant_downloa ...

  2. linux 解决磁盘占用100%

    df -h 查看磁盘使用情况 ll -h 查看文件的大小   使用如下命令查找大于100M的大文件,发现有几个日志文件及临时文件比较大,使用rm –rf删除即可.   find / -size +10 ...

  3. FoveaBox:细节差别,另一种DenseBox+FPN的Anchor-free方案 | IEEE TIP 2020

    作为与FCOS和FSAF同期的Anchor-free论文,FoveaBox在整体结构上也是基于DenseBox加FPN的策略,主要差别在于FoveaBox只使用目标中心区域进行预测且回归预测的是归一化 ...

  4. 流程控制、if、elif、else,whilie、break、continue的使用

    今日内容 流程控制理论 if判断 while循环 流程控制概念 流程控制就是控制事物的执行流程 执行流程的分类 顺序结构 从上往下依次执行,代码运行流程图如下 分支结构 根据某些条件判断做出不同的运行 ...

  5. 别再问WiFi密码了,HMS Core统一扫码服务让手机一键联网

    现代生活离不开网络.在餐厅.商场等公共场所,手机连接WiFi一直是高频使用场景.虽然公共场所的免费WiFi越来越多,但网络连接过程却很麻烦.有的需要打开网页注册或点击广告链接才能上网,还有的要求下载特 ...

  6. RENIX软件连接机箱测试RTSM基本操作——网络测试仪实操

    本文主要介绍了RENIX软件RTSM功能连接机箱测试的基本操作.文章分为三部分内容,第一部分为RTSM功能简介,第二部分为RTSM原理简介,第三部分为RTSM测试方案的具体介绍. 第一部分.RTSM功 ...

  7. ElasticSearch 分布式及容错机制

    1 ElasticSearch分布式基础 1.1 ES分布式机制 分布式机制:Elasticsearch是一套分布式的系统,分布式是为了应对大数据量.它的特性就是对复杂的分布式机制隐藏掉. 分片机制: ...

  8. 微信小程序缓冲类的封装

    1:utils 目录下新建一个Cache.js文件 2:文件下书写以下代码: // 缓存类 class Cache { // 构造方法 单位秒 constructor({ expire = 3600 ...

  9. 微信请求tp5框架数据 及渲染数据至页面

    tp模型数据: namespace app\xcx\model; use think\Model; class XcxModel extends Model { //链接数据库表名 protected ...

  10. 03 Java的数据类型分为两大类 类型转换 八大基本类型

    数据类型 强类型语言:要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用 Java的数据类型分为两大类 基本类型(primitive type) 数值类型 整数类型 byte占1个字节范围: ...