RSS Feed for This PostCurrent Article

Java: Always Overwrite equals, hashCode and toString in Value Object

You should always overwrite equals, hashCode and toString in your value objects, this will let you have more control over your value objects or model classes.

Also,  always overwrite hashCode and equals in pair. Consider the following code snippet

   1: import java.util.Set;
   2: import java.util.HashSet;
   3:  
   4: public class EqualTest {
   5:     private String name;
   6:  
   7:     public EqualTest(String name) {
   8:         this.name = name;
   9:     }
  10:  
  11:     public String getName() {
  12:         return name;
  13:     }
  14:  
  15:     public void setName(String name) {
  16:         this.name = name;
  17:     }
  18:  
  19:     public boolean equals(Object o) {
  20:         if (this == o) return true;
  21:         if (o == null || getClass() != o.getClass()) return false;
  22:  
  23:         EqualTest equalTest = (EqualTest) o;
  24:  
  25:         if (name != null ? !name.equals(equalTest.name) : equalTest.name != null) return false;
  26:  
  27:         return true;
  28:     }
  29:  
  30:     public String toString() {
  31:         return "EqualTest{" +
  32:                 "name='" + name + '\'' +
  33:                 '}';
  34:     }
  35:  
  36:  
  37:     public static void main(String[] args) {
  38:         EqualTest equalTest = new EqualTest("jason");
  39:  
  40:         Set<EqualTest> set = new HashSet<EqualTest>(1);
  41:         set.add(equalTest);
  42:         
  43:         if (set.contains(new EqualTest("jason"))) {
  44:             System.out.println("true");
  45:         } else {
  46:             System.out.println("false");
  47:         }
  48:  
  49:     }
  50: }

In the above code, hashCode is not overridden. The result is “false”.

To get the expected results, you must also override hashCode method.

   1: import java.util.Set;
   2: import java.util.HashSet;
   3:  
   4: public class EqualTest {
   5:     private String name;
   6:  
   7:     public EqualTest(String name) {
   8:         this.name = name;
   9:     }
  10:  
  11:     public String getName() {
  12:         return name;
  13:     }
  14:  
  15:     public void setName(String name) {
  16:         this.name = name;
  17:     }
  18:  
  19:     public boolean equals(Object o) {
  20:         if (this == o) return true;
  21:         if (o == null || getClass() != o.getClass()) return false;
  22:  
  23:         EqualTest equalTest = (EqualTest) o;
  24:  
  25:         if (name != null ? !name.equals(equalTest.name) : equalTest.name != null) return false;
  26:  
  27:         return true;
  28:     }
  29:  
  30:     public int hashCode() {
  31:         return (name != null ? name.hashCode() : 0);
  32:     }
  33:  
  34:  
  35:     public String toString() {
  36:         return "EqualTest{" +
  37:                 "name='" + name + '\'' +
  38:                 '}';
  39:     }
  40:  
  41:  
  42:     public static void main(String[] args) {
  43:         EqualTest equalTest = new EqualTest("jason");
  44:  
  45:         Set<EqualTest> set = new HashSet<EqualTest>(1);
  46:         set.add(equalTest);
  47:         
  48:         if (set.contains(new EqualTest("jason"))) {
  49:             System.out.println("true");
  50:         } else {
  51:             System.out.println("false");
  52:         }
  53:  
  54:     }
  55: }


Trackback URL


RSS Feed for This Post1 Comment(s)

  1. liliam | Jan 23, 2010 | Reply

    I don’t get the point… Method HashSet.contains() is intended to check whether a given object is included in that set or not, obviously if you create a new object when checking it will return false. If you invoke contains() with equalTest reference just created before, you will get a true… isn’t it like it’s meant to be?.

Sorry, comments for this entry are closed at this time.