0

I created a class Person (as the book says) to hold the name and last name of a person entered from the keyboard and then there is another class PhoneNumber which encapsulates the country code, area code and the number of a person as a String.
Person is intended to be used as the key in a Hashmap.
Class BookEntry encapsulates both Person and PhoneNumber. A lot of BookEntry objects make up a HashMap that represents a phonebook.

Person implements Comparable<Person> so it contains CompareTo(Person) method. Later the book adds equals(Object anotherPerson)method.
My question is, isn't the CompareTo method enough for comparing two keys? or is it that the internal mechanics of the HashMap<> requires me to include equals() method to compare two keys?
compareTo()

public int compareTo(Person person) {
    int result = lastName.compareTo(person.lastName);
    return result==0? firstName.compareTo(person.firstName):result;
}

equals()

public boolean equals(Object anotherPerson){
    return compareTo((Person)person)==0;
}
Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
A User
  • 375
  • 2
  • 4
  • 13

3 Answers3

2

Some data structures will use compareTo (for example a TreeMap) and some will use equals (for example a HashMap).

More importantly, it is strongly recommended that compareTo and equals be consistent, as explained in the Comparator javadoc:

It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."

Another hint, found in TreeMap javadoc (emphasis mine):

Note that the ordering maintained by a tree map, like any sorted map, and whether or not an explicit comparator is provided, must be consistent with equals if this sorted map is to correctly implement the Map interface.

Finally, if you override equals you should also override hashcode to prevent unexpected behaviours when using hash-based structures.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • so If I do not know which data structure uses what, I can use a combination of both, as in my case :) – A User Jun 06 '12 at 11:26
  • 1
    @FasihKhatib Yes, if your class implements `Comparable`, it is good practice to override `equals` and `hashcode` consistently with your `compareTo` implementation. It will save you head scratching bugs later on. But your class **does not need to implement `Comparable`** to be used in a `HashMap` if that's your question. – assylias Jun 06 '12 at 11:29
1

compareTo() method is used in sorting,

This method's implementation will determine who is greater(lesser, same) between two person, also at what degree

while equals() & hashcode() will be used in Hash based data structure (HashMap) in your case

user-defined class as a key of HashMap

yes you need to implement hashcode() and equals() properly

Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • so basically, If i am using a user-defined class as a key then it **must** have an `equals()` and a `compareTo()` method to be used by internal mechanics, if i get it correctly :) – A User Jun 06 '12 at 11:08
  • _user-defined class as a key_ of `HashMap` yes you need to implment `hashcode()` and `equals()` properly – jmj Jun 06 '12 at 11:09
  • You must not necessarily have an equals() methods. By default, equals just uses == for equality, which is fine for a great number of cases. – Polygnome Jun 06 '12 at 11:10
  • 1
    @Polygnome not really See :http://stackoverflow.com/questions/5581913/integer-wrapper-class-and-operator-where-is-behavior-specified – jmj Jun 06 '12 at 11:12
1

HashMap uses equals() and not compareTo(), so you have to implement it. TreeMap uses compareTo().

adranale
  • 2,835
  • 1
  • 21
  • 39
  • but for `equals()` to work, I need `compareTo()` . makes sense now :) – A User Jun 06 '12 at 11:19
  • 2
    No, you don't necessarily need `compareTo()` for `HashMap`. You need `equals` and `hashCode` to be consistent. That is all. – Petar Minchev Jun 06 '12 at 11:21
  • 1
    Either you write the code in `compareTo()` and call it from within `equals()` or you write the code directly in `equals(). What is important is that your `equals()` should work properly. – adranale Jun 06 '12 at 11:47