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.
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:
1) Both ArrayList and HashMap are not synchronized, you should not use them in the multithreading environment without external synchronization.
2) 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.
3) Both ArrayList and HashMap allow null. HashMap allows null keys and values.
4) ArrayList allows duplicate elements and HashMap allow duplicate values.
5) 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.
6) ArrayList is backed by array similarly, HashMap is also internally implemented by Array.
7) 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.
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.
That's all about the difference 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
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)
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.
What is performance comparison on HashMap vs ArrayList. I mean if I want to retrieve an object whose index in ArrayList is known and key in HashMap is known, which one will be faster. How about addition time over long period ?
ReplyDeleteon use index and one use key so both are order specific so....both r n same category bt as speed point of view as arraylist requires less size its quit better than hash map
DeleteHow can you compare ArrayList with HashMap both are different kinds of data structure , Map holds two objects e.g. key and value while List only holds one object and that is data.
ReplyDelete@Anonymous, that's the difference and aticle says diference between ArrayList and HashMap. To add on that there are couple of others, which are worth noting :
Delete1) HashMap has load factor to decide when to resized but ArrayList doesn't have load factor. Capacity is checked when you add element using add() method and ArrayList is incresed by 50% or big enough to hold new elements along with old elements if resize happens.
Which one is better for storing and retrieval?
ReplyDeleteHello @Prachi, if you just want to store an object than use ArrayList, if you have a mapping e.g. a key and a value like emp_id mapped to Employee object than use HashMap.
Delete