简体   繁体   中英

key - value mapping in a HashMap

Whenever I wanted to use a general purpose data structure like a Set or Map in Java, I was presented with HashSet and HashMap. Upon searching more about these, I've read about the hash table that sits at the base of them. So I learned that, in order to map a key to its corresponding value, it uses a hash function.
My question is: why not use a simple by-index correspondence?
I tried to answer myself but I'm not sure of the correctness of the advantages:
by-index
+ faster, since there's no computation involved
+ collision-free
by-hash
+ memory economy, since a third array would be needed to correlate the keys and values

But I always thought that a few flops are more precious than a few bytes. Anyway, I don't think there is such a DS in Java as a simple IndexMap, is it? What am I missing here?

EDIT:
By by-index I mean this: an array is by construction an ordered structure, it has indices; so having 2 arrays to correlate, say, keys[] and values[] would mean keys[i] corresponds to values[i]. So, no relation function needed. I'm surely missing something here and I want to see what.

You can think of a HashMap as IndexMap where the index can be any Object (key) and it is mapped to another Object (value). An IndexMap in your sense would be just a List, a List maps an int index to an Object. But the indexes of a List cannot be Objects.

why not use a simple by-index correspondence?

Because this would break the Set contract for that particular implementation; in a Set , all values must be unique according to a defined criterion. In HashSet , this is the equality (as in .equals() / .hashCode() ) of objects.

Also, a Set has no iteration order guarantee -- again, by contract. Therefore you cannot use indices to locate anything in a Set , even if you know its .size() (unlike in a List , which looks more like what you want, you will notice that a Set has no .get() method).

Note that keys in a Map are also a Set ; keys in a Map are unique.

Another advantage of using a hash table is the speed of lookup operations (as in .contains() ).

I'm going to take a stab at what your trying to ask. Why are Java HashMap not like EnumMap s or other special hashtable collections that do not require a hash (because the hash is pre-calculated).

Its because HashMap is a generic collection so that you can store any object. If you want a faster HashMap (ie fast hash) then certain assumptions need to be made about the data your storing in the Map (like EnumMap ).

I are in right direction but need to consider few more actions that you might want to perform on your data structure.

accessing in array costs o(1) which is same as in hash based data structure but searching could be very-very costly o(n) in array where as it is still same o(1) in hash based structure.

While deciding on which data structure is good for your application it depends what you are going to do on that data structure. For further reference please check this page which contains summary of different operation on different data structures:

http://simplenotions.wordpress.com/2009/05/13/java-standard-data-structures-big-o-notation/

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM