Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hash table internal index

How can I reach the internal array representation of the Java Hashtable? I know that hashtables are just cleverly organized arrays and I want to use the index of each key so I can work in parallel with a Disjoint set.

I need one of two things:

  1. The hash function used to turn my keys into the index in the internal array of the hashtable
  2. The corresponding index of the key.
like image 548
Ben Kellman Avatar asked Feb 23 '26 09:02

Ben Kellman


1 Answers

Note 1: In 90% of the cases you should probably use the HashMap class instead of Hashtable.

Note 2: Actually most hash tables combine a list with an array, in order to handle hash collisions.

In general you are not supposed to be able to reach the internals of the classes that come with the Java implementation. That would defeat the whole purpose of Java providing the ability for a clear separation between interface and implementation.

I would suggest, instead, that you create a new class e.g. MyHashMap by copying over the source code of the HashMap implementation from your Java Development Kit. By having your own copy you have full control and you are not subject to unexpected breakage after a JRE update. You can find the OpenJDK HashMap implementation here.

PS: You could theoretically try accessing the HashMap internals using reflection, but the resulting code would be atrocious and the performance even worse...

like image 108
thkala Avatar answered Feb 24 '26 23:02

thkala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!