-1
  • What happens if you design a new class and try to insert objects of that class into a HashSet or HashMap without defining a hashCode() method?

Please keep make the explanation easy. I'm studying for an exam and I'm still rusty with hashes in Java. Thank you.

Amin J
  • 1,179
  • 9
  • 16
K. H. Reiter
  • 3
  • 1
  • 2
  • 2
    Can you not test this yourself with just a few lines of code? – andrel Sep 24 '16 at 21:25
  • Look at [this](http://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java). Is is a problem specially for a `HashSet` if you don't override `hashCode()` since sets are supposed to not have duplicates and without override's every object is going to be assumed unique (even if they are not). – Amin J Sep 25 '16 at 04:17

2 Answers2

1

A HashMap stores data into multiple singly linked lists of entries (also called buckets or bins). All the lists are registered in an array of Entry (Entry[] array)

The following picture shows the inner storage of a HashMap instance with an array of nullable entries. Each Entry can link to another Entry to form a linked list.

When a user calls put(K key, V value) or get(Object key), the function computes the index of the bucket in which the Entry should be. enter image description here

This index of the bucket (linked list) is generated using hashcode of the key. So, if you have overridden the hashCode method, it will use overridden method to compute index of the bucket otherwise default hash code is used which is the memory address for your object. So in that case even your objects are you will have a new entry in your map. So even if you try to store logically equal objects. They wil be reataed as different by hash Map.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

For example:

MyObject a = new MyObject("a", 123,"something");
MyObject b = new MyObject("a", 123,"something");
a and b will have different hashcodes.
Shweta Gulati
  • 566
  • 1
  • 7
  • 17
0

Nothing will happen :-)

Every object has own hashCode() method that inherited from Object class. So, your every new object will be unique. By herself, they will be identified as unique by HashSet or HashMap.

Here are official comments:

/**
 * Returns a hash code value for the object. This method is
 * supported for the benefit of hash tables such as those provided by
 * {@link java.util.HashMap}.
 * <p>
 * The general contract of {@code hashCode} is:
 * <ul>
 * <li>Whenever it is invoked on the same object more than once during
 *     an execution of a Java application, the {@code hashCode} method
 *     must consistently return the same integer, provided no information
 *     used in {@code equals} comparisons on the object is modified.
 *     This integer need not remain consistent from one execution of an
 *     application to another execution of the same application.
 * <li>If two objects are equal according to the {@code equals(Object)}
 *     method, then calling the {@code hashCode} method on each of
 *     the two objects must produce the same integer result.
 * <li>It is <em>not</em> required that if two objects are unequal
 *     according to the {@link java.lang.Object#equals(java.lang.Object)}
 *     method, then calling the {@code hashCode} method on each of the
 *     two objects must produce distinct integer results.  However, the
 *     programmer should be aware that producing distinct integer results
 *     for unequal objects may improve the performance of hash tables.
 * </ul>
 * <p>
 * As much as is reasonably practical, the hashCode method defined by
 * class {@code Object} does return distinct integers for distinct
 * objects. (This is typically implemented by converting the internal
 * address of the object into an integer, but this implementation
 * technique is not required by the
 * Java&trade; programming language.)
 *
 * @return  a hash code value for this object.
 * @see     java.lang.Object#equals(java.lang.Object)
 * @see     java.lang.System#identityHashCode
 */
public native int hashCode();
Mirjalol Bahodirov
  • 2,053
  • 1
  • 11
  • 16