开发者

How do I implement a member wise comparison in java?

I'm from a C++ background and just started Java today. Say I have a class with a couple of data members. For example:

public class Person {
    //Constructors/Destructor
    public Person(Str开发者_Go百科ing strFirstName, String strLastName) {
        m_strFirstName = strFirstName;
        m_strLastName = strLastName;
        m_strFullName = m_strFirstName + m_strLastName;
    }

    //Getters
    public String GetFullName() { return m_strFullName; }
    public String GetFirstName() { return m_strFirstName; }
    public String GetLastName() { return m_strLastName; }

    //Private Data Members
    private String m_strFirstName;
    private String m_strLastName;
    private String m_strFullName;
}

Now lets say I do this:

Person john = new Person("john", "doe");
Person johndoe = new Person("john", "doe");
if (john == johndoe) {
    System.out.println("They are Equal");
} else {
    System.out.println("They are NOT Equal");
}

Here the result is "They are NOT Equal". I understand this is because Java is comparing the references (memory addresses), and since they are different locations in memory the test fails. I have read that Java doesn't support operator overloading, so I can't overload the operator==, so is there a method I'd override to implement my memberwise comparison? The object.equals method looked promising, but I've read that it's bad practice ot override this one.

UPDATE: Ok I'm convinced overriding equals is OK! I can't find that article that said that it's bad. Thanks for the help, I'll probably have more questions as I learn Java!!


You do:

if (john.equals(johndoe)) {
  ...
}

and implement the equals() method on your object:

public class Person {
  private String firstName;
  private String lastName;
  private String fullName;

  public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.fullName = firstName + lastName;
  }

  public String getFirstName() { return firstName; }
  public String getLastName() { return lastName; }
  public String getFullName() { return fullName; }

  @Override
  public boolean equals(Object ob) {
    if (ob == null) return false;
    if (ob.getClass() != getClass()) return false;
    Person other = (Person)ob;
    if (!firstName.equals(other.firstName)) return false;
    if (!lastName.equals(other.lastName)) return false;
    if (!(fullName.equals(other.fullName)) return false;
    return true;
  }

  @Override
  public int hashCode() {
    return firstName.hashCode() ^ lastName.hashCode() ^ fullName.hashCode();
  }
}

Two things to note:

  1. The above is more Java-style than C++-style. I'd highly suggest you adopt Java coding conventions when coding Java. If you're new to Java, you should try and learn them; and
  2. Java has an equals/hashCode contract meaning that if two objects are equal then their hash codes must be equal.


Where did you read that it was poor practice? It is very common to overload equals (and hashCode) in Java. See What issues should be considered when overriding equals and hashCode in Java?.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜