• Java equals()和hashCode()重写总结


      

      在实际开发中有时候会遇到需要比较同一个类的不同实例对象的场景,一般情况下继承自Object父类的 equals()和hashCode()可以满足需求,但却不能满足所有的场景,比如只需要使用少数几个对象属性来判断 比较是否是同一个对象,这时我们就需要自定义的equals()和hashCode()实现来进行重写覆盖Object中的方 法。

      1. equals()方法重写注意事项

      a. 自反性:对于任意的引用值x,x.equals(x)一定为true。

      b. 对称性:对于任意的引用值x 和 y,当x.equals(y)返回true时, y.equals(x)也一定返回true。

      c. 传递性:对于任意的引用值x、y和z,如果x.equals(y)返回true,

      并且y.equals(z)也返回true,那么x.equals(z)也一定返回true。

      d. 一致性:对于任意的引用值x 和 y,如果用于equals比较的对象信息没有被修

      改,多次调用x.equals(y)要么一致地返回true,要么一致地返回false。

      e. 非空性:对于任意的非空引用值x,x.equals(null)一定返回false。

      2. 广州java课程重写equals()方法的同时一定要重写hashcode()方法

      hashcode是用于散列数据的快速存取,如利用HashSet/HashMap/Hashtable类来存储数据时,都是根据存
    储对象的hashcode值来进行判断是否相同的。

      如果重写equals()的时候没有重写hashcode()方法,就会造成对象之间的混淆。

      equals()和hashcode()方法之间要满足一下关系:

      (1)当obj1.equals(obj2)为true时,obj1.hashCode() == obj2.hashCode()必须为true

      (2)当obj1.hashCode() == obj2.hashCode()为false时,obj1.equals(obj2)必须为false

      3. 示例代码

      package com.vdlm.dal.model;

      public class UserViewScore {

      private Integer totalScore;

      private String taskContentId;

      private String userId;

      private String type;

      public String getTaskContentId() {

      return taskContentId;

      }

      public void setTaskContentId(String taskContentId) {

      this.taskContentId = taskContentId;

      }

      public String getUserId() {

      return userId;

      }

      public void setUserId(String userId) {

      this.userId = userId;

      }

      public String getType() {

      return type;

      }

      public void setType(String type) {

      this.type = type;

      }

      public boolean equals(Object object) {

      if (this == object) {

      return true;

      }

      if (object == null) {

      return false;

      }

      if (getClass() != object.getClass()) {

      return false;

      }

      UserViewScore userViewScore = (UserViewScore)object;

      if (userViewScore.getUserId() == null || userViewScore.getTaskContentId()== null) {

      return false;

      }

      return userId.equals(userViewScore.getUserId()) && taskContentId.equals
    (userViewScore.getTaskContentId());

      }

      public int hashCode(){

      int result = userId.hashCode()+taskContentId.hashCode();

      return result;

      }

      public Integer getTotalScore() {

      return totalScore;

      }

      public void setTotalScore(Integer totalScore) {

      this.totalScore = totalScore;

      }

      }

      单元测试:

      public class UserViewScoreTest {

      @Test

      public void test() {

      UserViewScore user1 = new UserViewScore();

      user1.setUserId("111");

      user1.setTaskContentId("123");

      UserViewScore user2 = new UserViewScore();

      user2.setUserId("111");

      user2.setTaskContentId("123");

      UserViewScore user3 = new UserViewScore();

      user3.setUserId("111");

      user3.setTaskContentId("123");

      //验证重写equals的对称性

      System.out.println("user1.equals(user2):"+user1.equals(user2));

      System.out.println("user2.equals(user1):"+user2.equals(user1));

      //验证重写equals的传递性

      System.out.println("user1.equals(user2):"+user1.equals(user2));

      System.out.println("user2.equals(user3):"+user2.equals(user3));

      System.out.println("user1.equals(user3):"+user1.equals(user3));

      //验证重写equals的自反性

      System.out.println("user1.equals(user1):"+user1.equals(user1));

      //验证重写equals的非空性

      System.out.println("user1.equals(null):"+user1.equals(null));

      }

      }

  • 相关阅读:
    【网易官方】极客战记(codecombat)攻略-地牢-矮人骚乱
    Digital Twin——IoT的下一个浪潮
    PPT |《Kubernetes的兴起》
    视频课程 | Kubernetes的兴起
    干货 | 京东云原生容器—SpringCloud实践(一)
    干货 | 独创分布式网络负载均衡最佳实践
    视频课程 | 云原生下的Serverless浅谈
    ubuntu不能联网的问题
    boost库在windows上的安装
    python tkinter
  • 原文地址:https://www.cnblogs.com/gojava/p/4589252.html
Copyright © 2020-2023  润新知