This question already has an answer here:
List vs Map in Java
In Java, Array and HashMap are used as collections.But I couldn’t understand in which situations we should use Array and which time to use HashMap.
What is the major difference between both of them?
I’m confused about the difference between the Array and HashMap.
Can anyone explain this to me?
- Array has values, HashMap has keys and values. Whereas you would access a value in array with something like this array, you can’t do that with a HashMap. You have to call hashmap.get(key) to retrieve a desired value (which means you need a key to directly access the associated value).
- HashMaps cannot have repeating keys. Therefore, if you have a HashMap of keys and values, you can be sure that its keys are unique (values may contain duplicates across the key-value pairs).
- Arrays will maintain your order, and you can sort them. HashMap makes no guarantees on what order will be used. It also makes no guarantees that the order will remain constant over time. So if you’re looking for it, you might be better off with LinkedHashMap.
- While it is necessary to know a key to retrieve a value from HashMap, you also have a contains(key) method, which is O(1) with a properly implemented hash. It allows you to check whether a key exists in a map. It is quite useful if you store associated data and want to check whether you already have a desired key.
What both of them do have in common is that both get and put operations are (in ideal case) O(1).
Another tidbit of info: if I’m not mistaken, HashMaps utilize arrays (and LinkedLists) underneath. It’s a very useful data structure, and quite ingenious, too.
You’d want to use HashMaps in situations where you want to associate two pieces of data with one another. Say, you have a Restaurant and an Address. You could use Restaurant as a key and Address as a value (not the best example there is, but you get the point). As for arrays, if you have a list of your favorite restaurants, array might be a good choice to keep them.
Assuming you are perfectly aware of the fact that a HashMap handles key-value pairs while an array does not..
A HashMap uses a keys HashCode to decide at what index to store the value in an array. When you give a HashMap a key in order to get the associated value, it will first calculate key.hashcode() & m, where m is the length of the underlying array, and then inspect what’s stored at that location.
One implementation of a HashMap might store linked lists at each array spot. If this is the case, the HashMap would now traverse the list and return a value v if the associated key and the key you provided are equal (determined by the equals() method).
The need to store Linkedlists at each arrayspot arises from the use of %. A hashcode could be very large, larger than the size of the array. This means that several different keys could end up generating the same indexes.
A big difference is the time complexities. In worst case a lookup in a HashMap will take O(n) time. This happens if the HashCode is implemented poorly and all the items end up with the same HashCode. In an array however, a lookup is always constant time O(1).
I hope this makes it somewhat clearer to you.