码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-06-19 18:38:29      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

  

  在实际开发中有时候会遇到需要比较同一个类的不同实例对象的场景,一般情况下继承自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));

  }

  }

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

标签:

原文地址:http://www.cnblogs.com/gojava/p/4589252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!