Difference between ArrayList and HashMap in Java

Difference between ArrayList and HashMap in Java
One of the most critical difference between HashMap and ArrayList class is that former is the implementation of the hash table while later is a dynamic array which can resize itself. The HashMap and ArrayList are two of the most popular classes from Java Collection framework. Though both are used to store objects but they are completely different on their implementation, working and usage. The main difference between ArrayList and HashMap is that ArrayList is index based data-structure backed by an array while HashMap is map data structure which works on hashing to retrieve stored values. This is also one of the frequently asked Java Collection interview questions, which is often asked 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 similarity between ArrayList and HashMap in Java.


Similarity between ArrayList and HashMap in Java

Here are some of the most common similarities between ArrayList and HashMap in Java:

1) BothArrayList and HashMap are not synchronized, you should not use them in the multi-threading 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 allows null. HashMap allows null key and values.

4) ArrayList allows duplicate elements and HashMap allow duplicate values.

5) In terms of performanceArrayList 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 similarity between ArrayList and HashMap in Java:

Similarity between HashMap and ArrayList 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 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 implements equals and hashCode method correctly, ArrayList doesn't have that requirement but its good to have that because contains()  method of ArrayList will use 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 object, 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 which highlights they key difference between ArrayList and HashMap in Java:

Difference between ArrayList and HashMap in Java


That's all about the difference between ArrayList and HashMap in Java. They both are completely different to each other and exist for a different purpose.Use HashMap if you need 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)

Further Reading
Java Programming Interview Exposed
Core Java, Volume 1 by Cay S. Horstmann


6 comments:

  1. 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 ?

    ReplyDelete
    Replies
    1. on 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

      Delete
  2. How 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
    Replies
    1. @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 :
      1) 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.

      Delete
  3. Which one is better for storing and retrieval?

    ReplyDelete
    Replies
    1. Hello @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