2

I was reading an answer to a question asked here: Why does hashcode() returns an integer and not long?

My question is: Why hashcode based data structures use an array to create bins?

  • Since the prime use of hashCode() is to determine which slot to insert an object into, arrays prove to be the easiest "low-level" data structure having constant random access. – dgupta3091 Sep 06 '19 at 09:19
  • Indexed access of an array element is a small constant cost: **O(1)**, Hence `array[hashcode % array,length]` is ideal. A tree would need **O(log n)**. – Joop Eggen Sep 06 '19 at 12:42

2 Answers2

1

Because array is a low-level data structure which allows random access to its elements.

You need a "low-level" data structure to base a "higher-level" data structure on.

You need random access so that you can address bins very fast.

lexicore
  • 42,748
  • 17
  • 132
  • 221
0

cause, an array is based on integer-based indexes! now you can show some curiosity, why array using integer-based indexing. one of the assumptions should be -- if you could able to use other types (real numbers) rather than using integer, just think how many dimension you would capable to add -- for example --

for 1-th index, you could capable to add sub-indexes like -- 1.1, 1.2, 1.1.2, 1.1.1.1.2 and so on so forth! 

doing so, it will create more overhead, rather than popping up the solution we want.

Papai from BEKOAIL
  • 1,469
  • 1
  • 11
  • 17