---
title: "重载equals方法就一定要要重载hashCode方法"
author: "songhongchen"
date: "2009-04-10T14:04:37Z"
tags: [".net", "Bean", "Blog", "Java"]
categories: ["非技术"]
slug: "equals-hashcode"
draft: false
lastmod: "2009-04-10T14:07:44Z"
id: 365029
url: "https://songhongchen.iteye.com/blog/365029"
---

在进行对象之间的比较的时候，一般都会override equals方法，而hashCode的override很多时候都会被忽略。hashCode方法一般都被认为是用来判断两个对象是不是同一个引用的，它返回一串散列码，这个散列码的主要用途是进行Hash存储时，作为键用的，加快查找对象的速度。一般都会以31 29这样的素数作为基数，主要目的是为了减少在一个大的集合中键值的重复，导致冲突的情况发生，以提高性能。 有关深入HashCode方法详见：http://blog.csdn.net/axman/archive/2004/11/26/195159.aspx 下面有个Bean，在只重载了equals方法后，执行插入Set后的个数和重载了equals与hashCode方法两个方法后的Set集合中的对象个数： \[code="java"]import java.util.Date; import java.util.HashSet; import java.util.Set; public class ListSet { public static void main(String... a) { Set set = new HashSet(); Date date = new Date(); UserBean ub; for (int i = 0; i &lt; 20; i++) { ub = new UserBean(); ub.setUsername("javaeye"); ub.setPassword("javaeye"); ub.setSex(i % 2); ub.setBirthDay(date); set.add(ub); } System.out.println("Set中的UserBean对象个数:" + set.size()); } } class UserBean { private String username; private String password; private int sex; private Date birthDay; @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final UserBean other = (UserBean) obj; if (birthDay == null) { if (other.birthDay != null) return false; } else if (!birthDay.equals(other.birthDay)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (sex != other.sex) return false; if (username == null) { if (other.username != null) return false; } else if (!username.equals(other.username)) return false; return true; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } }\[/code] 执行结果是：Set中的UserBean对象个数:20 而加了hashCode方法后： \[code="java"]import java.util.Date; import java.util.HashSet; import java.util.Set; public class ListSet { public static void main(String... a) { Set set = new HashSet(); Date date = new Date(); UserBean ub; for (int i = 0; i &lt; 20; i++) { ub = new UserBean(); ub.setUsername("javaeye"); ub.setPassword("javaeye"); ub.setSex(i % 2); ub.setBirthDay(date); set.add(ub); } System.out.println("Set中的UserBean对象个数:" + set.size()); } } class UserBean { private String username; private String password; private int sex; private Date birthDay; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((birthDay == null) ? 0 : birthDay.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + sex; result = prime * result + ((username == null) ? 0 : username.hashCode()); 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 UserBean other = (UserBean) obj; if (birthDay == null) { if (other.birthDay != null) return false; } else if (!birthDay.equals(other.birthDay)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (sex != other.sex) return false; if (username == null) { if (other.username != null) return false; } else if (!username.equals(other.username)) return false; return true; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } }\[/code] 执行结果是：Set中的UserBean对象个数:2