Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashtable values displayed in Eclipse debugger

enter image description hereI 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.

like image 583
ling Avatar asked Sep 15 '14 22:09

ling


1 Answers

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$EntryObject with a reference to next Map$EntryObject 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.

like image 143
Kumar Abhinav Avatar answered Sep 30 '22 18:09

Kumar Abhinav