Why study hashCode()
and equals()
here? Cause they are essential to working with Java Collections Framework. Whenever you need to sort or search through a collection of objects, the equals()
and hashCode()
methods are essential.
-
equals()
decides whether two objects are meaningfully equivalent.==
operator just compares two object references. It simply looks at the bits in the variables and verify if they are the same or not.- If we don't override a class's
equals()
method, we won't be able to use those objects as a key in a hashtable and we won't be able to get accurate Sets such that does not allow duplicated object. - If we want objects of our class can 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), so we must override
equals()
so that two different instances can be considered the same. - The
equals()
contract:- Reflexive:
x.equals(x)
== true - Symetric:
x.equals(y)
theny.equals(x)
- Transitive:
x.equals(y)
andy.equals(z)
thenx.equals(z)
- For non-null reference x then
x.equals(null)
should return false
- Reflexive:
-
hashCode()
is used by some collections to specify the way how an object is stored in the collection and how it is located from the collection.hashCode()
implements the hash function and it should use the same instance variables that theequals()
method uses.- The
hashCode()
contract:x.equals(y)
returns true thenx.hashCode()
==y.hashCode()
- But
x.hashCode()
==y.hashCode()
thenx.equals(y)
may not return true - And
x.hashCode()
!=y.hashCode()
thenx.equals(y)
must return false
- The
-
Collections Overview
- Ordered When a collection is ordered, it means you can iterate through the collection in a specific order. A
Hashtable
is not an ordered collection although it has its internal logic to determine the order of iteration. AnArrayList
keeps the order established by the element's index (just like an array). ALinkedHashSet
keeps the order established by insertion so the last element inserted is the last element in thatLinkedHashSet
. - Sorted Use
Comparator
andComparable
to define sort order to sort a collection.
- Ordered When a collection is ordered, it means you can iterate through the collection in a specific order. A
-
List Interface
-
Set Interface
-
Map Interface
-
Queue Interface