Example: TestEqualLists1.java
Sometimes this is what you want, but often it is not. For example, you would like two linked lists to be considered equal if they have the identical sequence of elements; and that this sense of ``equals'' should be used by all the functions, including library functions, that call the equals method. Likewise, you might like to use a list like [1, 5, 8] as a key, and then look it up with a different list [1, 5, 8], without it having to be the same actual object.
The solution is to override the equals(X) and the hashCode() methods: Example using lists of ints: TestEqualLists2.java
Your new equals(X) and hashCode() methods must satisfy the following constraints; otherwise the functions that call these (lots of things, in the case of equals(); hash tables in the case of hashCode()) will fail in strange and unpredictable ways.
The method equals(X) must be an equivalence relation. That is:
The method hashCode() must be compatible with equals(X). That is, if X.equals(Y) returns true, then X.hashCode() and Y.hashCode() must be equal.
If you do want equality in the sense of ``the identical object'', then you can always use X == Y.
For complex data structures or mathematical entities, the question of what it means for two things to be the same, and how you compute that can be a deep and difficult one.
Using Hashing for Large Sets of Large Objects Note: This will not be on either the problem sets or the exams.