Difference between ArrayList and HashMap in Java (original) (raw)
Difference between ArrayList and HashMap in Java
One of the most critical differences between the HashMap and ArrayList class is that the former is the implementation of the hash table while the latter is a dynamic array that can resize itself. The HashMap and ArrayList are two of the most popular classes from the Java Collection framework. Though both are used to store objects they are completely different in their implementation, working, and usage. The main difference between ArrayList and HashMap is that ArrayList is an index-based data structure backed by an array while HashMap is a map data structure that works on hashing to retrieve stored values.
This is also one of the frequently asked Java Collection interview questions, which is often asked by Java developers of 1 to 3 years of experience. Apart from this fundamental difference between ArrayList and HashMap, there are many other differences, which we will see in this Java tutorial.
But before that let's see some similarities between ArrayList and HashMap in Java.
ArrayList and HashMap in Java
Here are some of the most common similarities between ArrayList and HashMap in Java:
Both ArrayList and HashMap are not synchronized, you should not use them in the multithreading environment without external synchronization.
Both ArrayList and HashMap Iterator are fail-fast, they will throw ConcurrentModificationException as soon as they detect any structural change in ArrayList or HashMap once Iterator is created.
Both ArrayList and HashMap allow null. HashMap allows null keys and values.
ArrayList allows duplicate elements and HashMap allow duplicate values.
In terms of performance ArrayList get() provide constant time performance if you know index similar to get() method of HashMap which provides constant-time performance.
ArrayList is backed by array similarly, HashMap is also internally implemented by Array.
Both ArrayList and HashMap can be traversed through Iterator in Java.
Here is a nice summary of similarities between ArrayList and HashMap in Java:
Difference between ArrayList vs HashMap in Java
Now it's time to see crucial differences between ArrayList vs HashMap in Java. This will help you to decide which Collection class is appropriate for a particular use case:
1. The first difference between ArrayList and HashMap is that ArrayList implements a List interface while HashMap implements Map interface in Java. See the difference between list and map for more information.
2. The second difference between ArrayList and HashMap is that ArrayList only stores one object while HashMap stores two objects key and value.
3. The third difference between HashMap and ArrayList is that keys of HashMap must implement equals and hashCode method correctly, ArrayList doesn't have that requirement but its good to have that because contains() method of ArrayList will use the equals() method to see if that object already exists or not.
4. The fourth difference between HashMap and ArrayList is that ArrayList maintains the order of objects, in which they are inserted while HashMap doesn't provide any ordering guarantee.
5. Another difference between ArrayList and HashMap is that ArrayList allows duplicates but HashMap doesn't allow duplicates key though it allows duplicate values.
6. ArrayList get(index) method always gives an O(1) performance but HashMap get(key) can be O(1) in the best case and O(n) in the worst case.
Here is a nice slide that highlights key difference between ArrayList and HashMap in Java:
That's all about the d ifference between ArrayList and HashMap in Java. They both are completely different from each other and exist for different purposes. Use HashMap if you need a map kind of structure to map keys to values and use ArrayList if you just looking to store objects in Java.
Related Java Collections Interview Questions
- The difference between HashSet and TreeSet in Java? (answer)
- The difference between ArrayList and HashSet in Java? (answer)
- The difference between Hashtable and HashMap in Java? (answer)
- The difference between HashMap and LinkedHashMap in Java? (answer)
- The difference between ConcurrentHashMap and HashMap in Java? (answer)
- The difference between ArrayList and Vector in Java? (answer)
- The difference between ArrayList and LinkedList in Java? (answer)
Thanks for reading this article so far. If you like this article then please share with your friends and if you have any questions please drop a note.
And lastly one question for you, what is the difference between HashMap, LinkedHashMAp, and TreeMap in Java? It's a popular Java interview question as well.