Define your own equals method : equals « Class Definition « Java Tutorial






class MyClass {
  private String name;

  MyClass(String name) {
    this.name = name;
  }

  public boolean equals(Object o) {
    if (!(o instanceof MyClass))
      return false;

    MyClass c = (MyClass) o;

    return name.equals(c.name);
  }
}

class EqualityDemo {
  public static void main(String[] args) {
    MyClass c1 = new MyClass("S");
    MyClass c2 = new MyClass("S");
    MyClass c3 = new MyClass("M");

    System.out.println("c1.equals (c2): " + c1.equals(c2));
    System.out.println("c1.equals (c3): " + c1.equals(c3));
  }
}








5.19.equals
5.19.1.Comparing Objects
5.19.2.Implement equals method using commons-lang
5.19.3.Use CompareToBuilder class to create compareTo method for your own class
5.19.4.Define your own equals method