There's a potential limitation lurking here: if you don't override a class's equals()
method, you won't be able to use those objects as a key in a hashtable and you
probably won't get accurate Sets, such that there are no conceptual duplicates.
The equals() method in class Object uses only the == operator for comparisons,
so unless you override equals(), two objects are considered equal only if the two
references refer to the same object.
Let's look at what it means to not be able to use an object as a hashtable key.
Imagine you have a car, a very specific car (say, John's red Subaru Outback as
opposed to Mary's purple Mini) that you want to put in a HashMap (a type of
hashtable we'll look at later in this chapter), so that you can search on a particular
car and retrieve the corresponding Person object that represents the owner. So you
add the car instance as the key to the HashMap (along with a corresponding Person
object as the value). But now what happens when you want to do a search? You want
to say to the HashMap collection, "Here's the car, now give me the Person object
that goes with this car." But now you're in trouble unless you still have a reference
to the exact object you used as the key when you added it to the Collection. In other
words, you can't make an identical Car object and use it for the search.
The bottom line is this: if you want objects of your class to be used as keys for a
hashtable (or as elements in any data structure that uses equivalency for searching
for—and/or retrieving—an object), then you must override equals() so that two
different instances can be considered the same. So how would we fix the car? You
might override the equals() method so that it compares the unique VIN (Vehicle
Identification Number) as the basis of comparison. That way, you can use one
instance when you add it to a Collection, and essentially re-create an identical
instance when you want to do a search based on that object as the key. Of course,
overriding the equals() method for Car also allows the potential that more than
one object representing a single unique car can exist, which might not be safe in your design. Fortunately, the String and wrapper classes work well as keys in
hashtables—they override the equals() method. So rather than using the actual
car instance as the key into the car/owner pair, you could simply use a String that
represents the unique identifier for the car. That way, you'll never have more than
one instance representing a specific car, but you can still use the car—or rather, one
of the car's attributes—as the search key.
Implementing an equals() Method
Let's say you decide to override equals() in your class. It might look like this:
public class EqualsTest {public static void main (String [] args) {Moof one = new Moof(8);Moof two = new Moof(8);if (one.equals(two)) {System.out.println("one and two are equal");}}}class Moof {private int moofValue;Moof(int val) {moofValue = val;}public int getMoofValue() {return moofValue;}public boolean equals(Object o) {if ((o instanceof Moof) && (((Moof)o).getMoofValue()== this.moofValue)) {return true;} else {return false;}}}
No comments:
Post a Comment