两种简单的方式快速实现hashCode 和 equals方法

http://hi.baidu.com/coolcat_police/item/8d248d02289a9bef359902a6

我们在写domain model类, 有时候需要重写hashCode和equals,以便可以把这个类当成List或者Map的Key来用。

    有两种方法可以很方便地去帮我们实现:

1. 使用eclipse工具。右击代码->Source->Generate hashCode and equals()...

    eclispe会根据你的model类里面的属性,帮你生成hashCode和equals方法,例如

public class Test implements Serializable {

private String id;

 

private String name;

 

public String getId() {

return id;

}

 

public void setId(String id) {

this.id = id;

}

 

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((id == null) ? 0 : id.hashCode());

result = prime * result + ((name == null) ? 0 : name.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;

Test other = (Test) obj;

if (id == null) {

if (other.id != null)

return false;

}

else if (!id.equals(other.id))

return false;

if (name == null) {

if (other.name != null)

return false;

}

else if (!name.equals(other.name))

return false;

return true;

}

 

}

 

 

2. 如果觉得hashCode和equals方法看上去不够简洁,可以使用org.apache.commons.lang.builder.HashCodeBuilder和org.apache.commons.lang.builder.EqualsBuilder

public class Test implements Serializable {

private String id;

 

private String name;

 

public String getId() {

return id;

}

 

public void setId(String id) {

this.id = id;

}

 

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

@Override

public int hashCode() {

return new HashCodeBuilder().append(this.id).append(this.name)

.toHashCode();

}

 

@Override

public boolean equals(Object obj) {

if (!(obj instanceof Test)) {

return false;

}

 

Test o = (Test) obj;

 

return new EqualsBuilder().append(this.id, o.id).append(this.name,

o.name).isEquals();

}

}

 

----------------------------------------------------------------------------------------------------------

 

this is another example:

 

  1. public class Student {  
  2.     private int age;  
  3.     private String name;  
  4.     public Student() {  
  5.     }  
  6.     public Student(int age, String name) {  
  7.         super();  
  8.         this.age = age;  
  9.         this.name = name;  
  10.     }  
  11.     public int getAge() {  
  12.         return age;  
  13.     }  
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.     public void setAge(int age) {  
  18.         this.age = age;  
  19.     }  
  20.     public void setName(String name) {  
  21.         this.name = name;  
  22.     }  
  23.     @Override  
  24.     public int hashCode() {  
  25.         final int prime = 31;  
  26.         int result = 1;  
  27.         result = prime * result + age;  
  28.         result = prime * result + ((name == null) ? 0 : name.hashCode());  
  29.         System.out.println("hashCode : "+ result);  
  30.         return result;  
  31.     }  
  32.     @Override  
  33.     public boolean equals(Object obj) {  
  34.         if (this == obj)  
  35.             return true;  
  36.         if (obj == null)  
  37.             return false;  
  38.         if (getClass() != obj.getClass())  
  39.             return false;  
  40.         Student other = (Student) obj;  
  41.         if (age != other.age)  
  42.             return false;  
  43.         if (name == null) {  
  44.             if (other.name != null)  
  45.                 return false;  
  46.         } else if (!name.equals(other.name))  
  47.             return false;  
  48.         return true;  
  49.     }  
  50.       

猜你喜欢

转载自speed847.iteye.com/blog/2010963