C
C
Cexhaif2016-08-21 14:25:39
Java
Cexhaif, 2016-08-21 14:25:39

How is data stored in a hash table?

We take the Java Object::hashCode as a hash function. Then (ignoring collision) we will have a maximum of Integer.MAX_VALUE (2,147,483,647) unique objects. And it turns out that we need to allocate an array of this size to store objects. But such an array, even an empty one, would take up a lot of memory. How is this problem solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2016-08-21
@Cexhaif

2,147,483,647

In a 32-bit number, ~4 billion values, not ~2.
The array is allocated for the amount of data, in different systems in different ways, for example, in python, when the array is ~ 70% full, then a larger array is allocated for this data.
The resulting hash is adjusted to the size of the array, for example, to 500 elements, an array of 1024 elements can be allocated, as a result, the resulting position can be calculated as follows = hash_value % mod table_length, example: 55555% 1024 -> 259, the total value will be written to cell 259 (if there is no conflict)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question