As per my understanding of ArrayList
, the default capacity is 10 and when it grows beyond 10, it will create a new object with new capacity and so on..
So out of curiosity, I typed following program to check hashcode()
for ArrayList
object:
public class TestCoreJava { public static void main(String [] args){ ArrayList al = new ArrayList(); for(int i=0;i<15;i++){ al.add("Temp"+i); System.out.println("Hashcode for "+i+" element "+al.hashCode()); } } }
According to above scenario, when I am not setting Initial capacity for ArrayList
the default would be 10. So while adding 11th element, it will create a new object and increase the capacity for ArrayList
.
When I print the hashcode for ArrayList
object, it is giving a new hashcode()
each time.
Following is the o/p:
Hashcode for 0 element 80692955 Hashcode for 1 element -1712792766 Hashcode for 2 element -1476275268 Hashcode for 3 element 1560799875 Hashcode for 4 element 1220848797 Hashcode for 5 element -727700028 Hashcode for 6 element -1003171458 Hashcode for 7 element -952851195 Hashcode for 8 element 607076959 Hashcode for 9 element 1720209478 Hashcode for 10 element -6600307 Hashcode for 11 element -1998096089 Hashcode for 12 element 690044110 Hashcode for 13 element -1876955640 Hashcode for 14 element 150430735
According to the concept of default capacity, till 10th element it should have printed same hashcode()
as no new object needs to be created until that point, but it is not the case.
Unlike, say, a HashMap, an ArrayList does not need to use the hashCode() method since the order of the elements in an ArrayList is determined by the order in which they were inserted, and not by hashing.
The purpose of the hashCode() method is to provide a numeric representation of an object's contents so as to provide an alternate mechanism to loosely identify it. By default the hashCode() returns an integer that represents the internal memory address of the object.
hashCode in Java is a function that returns the hashcode value of an object on calling. It returns an integer or a 4 bytes value which is generated by the hashing algorithm. The process of assigning a unique value to an object or attribute using an algorithm, which enables quicker access, is known as hashing.
The point is that hashcodes can be the same without necessarily guaranteeing that the objects are equal, because the "hashing algorithm" used in the hashCode() method might happen to return the same value for multiple objects.
The hashCode
of ArrayList
is a function of the hashCode
s of all the elements stored in the ArrayList
, so it doesn't change when the capacity changes, it changes whenever you add or remove an element or mutate one of the elements in a way that changes its hashCode.
Here's the Java 8 implementation (it's actually implemented in AbstractList
) :
public int hashCode() { int hashCode = 1; for (E e : this) hashCode = 31*hashCode + (e==null ? 0 : e.hashCode()); return hashCode; }
BTW, this is the exact code that appears in the Javadoc of hashCode()
of the List
interface :
int java.util.List.hashCode()
Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:
int hashCode = 1; for (E e : list) hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With