Why does the Collection interface have equals() and hashCode()?

why we need to override equals and hashcode in java
difference between equals and hashcode in javatpoint
equals() and hashcode contract
hashcode() and equals() method in java by durga
equals and hashcode in java interview questions
java override hashcode best practice
hashcode() and equals contract
this class overrides equals and should therefore also override hashcode

Why does the Collection interface have equals(Object o) and hashCode(), given that any implementation will have those by default (inherited from Object) ?


From the Collection JavaDoc:

While the Collection interface adds no stipulations to the general contract for the Object.equals, programmers who implement the Collection interface "directly" (in other words, create a class that is a Collection but is not a Set or a List) must exercise care if they choose to override the Object.equals. It is not necessary to do so, and the simplest course of action is to rely on Object's implementation, but the implementor may wish to implement a "value comparison" in place of the default "reference comparison." (The List and Set interfaces mandate such value comparisons.)

The general contract for the Object.equals method states that equals must be symmetric (in other words, a.equals(b) if and only if b.equals(a)). The contracts for List.equals and Set.equals state that lists are only equal to other lists, and sets to other sets. Thus, a custom equals method for a collection class that implements neither the List nor Set interface must return false when this collection is compared to any list or set. (By the same logic, it is not possible to write a class that correctly implements both the Set and List interfaces.)

and

While the Collection interface adds no stipulations to the general contract for the Object.hashCode method, programmers should take note that any class that overrides the Object.equals method must also override the Object.hashCode method in order to satisfy the general contract for the Object.hashCode method. In particular, c1.equals(c2) implies that c1.hashCode()==c2.hashCode().

Java hashCode() and equals(), Explains how the Java hashcode() and equals() method works, and what The specific contract rules of these two methods are best described in the JavaDoc. equals() is used in most collections to determine if a collection  Why does the Collection interface have equals() and hashCode() when we can directly use methods from Object class why again? Stack Overflow Products


To answer your specific question: why does it have these methods? It's done simply for convenience to be able to include Java Docs giving hints as to what implementers should do with these methods (e.g. comparing equality of values rather than references).

Understanding equals() and hashCode() in Java, However, you will have to override them specifically for the classes whose objects are added to collections, especially the hashtable-based collections such as The equals()method is designed to compare two objects Note that the List interface provides the contains(Object) method which can be used  Prerequisite – Equals and Hashcode method HashMap and HashSet use the hashcode value of an object to find out how the object would be stored in the collection, and subsequently hashcode is used to help locate the object in the collection.


To add to the other great answers. In the Collections interface, the equals method is defined in that interface to make some decisions in the way equaling two instances of collection should work. From the JAVA 8 documentation:

More generally, implementations of the various Collections Framework interfaces are free to take advantage of the specified behavior of underlying Object methods wherever the implementor deems it appropriate.

So you don’t add methods from the Object class for any other reason that giving more definitiveness to the java doc. This is the reason why you don’t count those methods in the abstract methods in the abstract methods of an interface.

Moreover, in JAVA 8, along the same line of reasoning, default methods from the Object class are not allowed and will generate a compile error. I believe it’s was done to prevent this type of confusion. So if you try to create a default method called hashCode(), for example, it will not compile.

Here is a more in-depth explanation for this behavior in JAVA 8 from the Lambda FAQ:

An interface cannot provide a default implementation for any of the methods of the Object class. This is a consequence of the "class wins" rule for method resolution: a method found on the superclass chain always takes precedence over any default methods that appear in any superinterface. In particular, this means one cannot provide a default implementation for equals, hashCode, or toString from within an interface.

This seems odd at first, given that some interfaces actually define their equals behavior in documentation. The List interface is an example. So, why not allow this?

One reason is that it would become more difficult to reason about when a default method is invoked. The current rules are simple: if a class implements a method, that always wins over a default implementation. Since all instances of interfaces are subclasses of Object, all instances of interfaces have non-default implementations of equals, hashCode, and toString already. Therefore, a default version of these on an interface is always useless, and it may as well not compile.

Another reason is that providing default implementations of these methods in an interface is most likely misguided. These methods perform computations over the object’s state, but the interface, in general, has no access to state; only the implementing class has access to this state. Therefore, the class itself should provide the implementations, and default methods are unlikely to be useful.

Working With hashcode() and equals(), If two objects are equal according to the equals(Object) method, then calling the hashcode() method on each of the two objects must produce  In Java, every object has access to the equals()method because it is inherited from the Object class. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. You must override hashCode()in every class that overrides equals().


Just to add to the great answers above, it makes sense to have the 'equals' or `hashCode' methods in this scenario:

Collection<Whatever> list1 = getArrayList();
Collection<Whatever> list2 = getAnotherArrayList();

if(list1.equals(list2)){
    // do something
}

In the absence of the equals method in the interface, we'll be forced to use concrete types, which is generally not a good practice :

ArrayList<Whatever> list1 = getArrayList();
ArrayList<Whatever> list2 = getAnotherArrayList();

if(list1.equals(list2)){
    // do something
}

Why can't default methods override equals, hashCode, and toString , Since all instances of interfaces are subclasses of Object, all instances of interfaces have non-default implementations of equals , hashCode  if the hashtables in the Java Collections API are to work correctly: If object1 and object2 are equal according to their equals()method, they must also have the same hash code. If object1 and object2 have the same hash code, they do NOT have to be equal too.


Java hashCode() and equals(), hashcode() of the object is only used during collections with hashing principle but if i don't have to use the object in any of the collections then is it  Hashcode value is mostly used in hashing based collections like HashMap, HashSet, HashTable….etc. This method must be overridden in every class which overrides equals() method. Syntax : public int hashCode() // This method returns the hash code value // for the object on which this method is invoked. The general contract of hashCode is:


Comparing Java objects with equals() and hashcode(), Java.lang.object has two very important methods defined: public boolean equals(​Object In java equals() method is used to compare equality of two Objects. Hashcode value is mostly used in hashing based collections like HashMap, HashSet, Arrays in Java · Access specifier of methods in interfaces · Inheritance and  In Java, every object has access to the equals()method because it is inherited from the Object class. If two objects are equal according to the equals(Object)method, then calling the hashCode method on each of the two objects must produce the same integer result. You must override hashCode()in every class that overrides equals().


equals() and hashCode() methods in Java, You must override hashCode() in every class that overrides equals(). properly in conjunction with all hash-based collections, including HashMap, HashSet, If two objects are equal according to the equals(Object) method, then calling the  Wish I could -1. If the Hashcode of a mutable object does not change, it will violate the equals contract, if two objects change from different (=hashcode doesn't matter) to equal (=hashcode absolutely must be the same, though it was most likely not the same before the change).