ar.thewaterfall
ar.thewaterfall

Reputation: 835

Why is it needed to override equals() and hashcode() using Set for many-value associations?

In hibernate manual it is said that you have to override the equals() and hashCode() methods if you intend to put instances of persistent classes in a Set (the recommended way to represent many-valued associations).

So the questions appeared:

Upvotes: 4

Views: 161

Answers (2)

Saurabh Oza
Saurabh Oza

Reputation: 179

Why is it a recommended way to represent many-value associations in a set?(Why shouldn't I use, let's say, ArrayList or LinkedList) -

Many relations are alway unique and there is no point in keeping duplicate values in a collection. As you know set always stores the unique value so Set is recommended to use.

What happens behind the scene that I should override those methods only when I use Set? -

Set stores only unique elements. Set uses Hashmap internally to store the element. So the bucket identification is done with the help of hashCode() and the elements are compared with the help of equals() method.

Upvotes: 0

Eugene
Eugene

Reputation: 120968

Well, how else would a HashSet deduce equality? This is based on hashCode/equals. For a Set you would need both methods, for a List equals is used only ( in a method like contains for example), but they are defined both, so that you don't run in weird surprises.

In general it is a good thing if you override compareTo from Comparable as-well - this is for example used internally for HasMap when a tie is found. You don't have to, but its somehow a good idea, if you ever plan to have these as keys in a Map.

That tie as I said is used when two hashCodes are equals and a HashMap decides to move to a perfectly balanced tree node, see here or this one ; or even this one

Upvotes: 2

Related Questions