I am debugging a Hashtable in Eclipse and found something strange. My Hashtable variable name is "my_hashTable", and Eclipse debugger, if I click it, it shows its values are three: {first=0, third=2, second=1}, which is correct as expected, and the count is 3, which is correct too.
However, if I click the "table" variable inside the my_hashTable variable, it shows there are only two non-null values, [4] = 2 and [5] = 0. Its full values are below:
[null, null, null, null, third=2, first=0, null]
Why does this happen? Where is the "second=1" pair? This is the first time I encountered this odd observation in Eclipse.
Any idea of what's going on? Thanks.
The above situation arises because of HashTable
structure where table array stores the key-value pairs in the form of Map$Entry
.If two keys hash to the same bucket using the Object hashcode() and the underlying Collection's hashing algorithm
,they would be put in the same bucket(say table[j] )in the form of a singly-linked list using the next reference present in each Map$Entry Object.Thus.each table index contains a Map$Entry
Object with a reference to next Map$Entry
Object having the same hashbucket(but whose keys are unequal as per the equals()
method of the Key Object).The thing is that the keys would be unequal as per the equals() method of the Key but their bucket index would be same using the hashcode() and hashing algorithm applied.
Just expand any of your visible table indexes for the next,and you would find the Key-value pair in the form of Map$Entry
.
Each Map$Entry stored in table array has the following structure:-
1)key
2)value
3)hashcode
4)next -contains reference of next Map$Entry
This is how Hash based collections work.Objects whose hashing algorithm return the same index are stored in the same bucket(table index) as a singular linked List.
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