Javatpoint Logo
Javatpoint Logo

Difference between ArrayList and HashMap in Java

In Java, ArrayList and HashMap are the two commonly used classes of the Java Collection Framework. Even by both are the part of the Collection framework, how they store and process the data is different. In the section, we will understand that what are the differences between ArrayList and HashMap.

The difference between ArrayList and HashMap is that ArrayList is an index-based data-structure supported by array, while the HashMap is a mapped data structure, which works on hashing to retrieve stored values. Although both are used to store objects, they are different in their implementation, function, and usage.

ArrayList vs. HashMap

It will help you to decide which collection class is suitable for a particular use:

ArrayList HashMap
ArrayList implements the List interface. HashMap implements the Map interface.
ArrayList implements the List interface. HashMap implements the Map interface.
ArrayList stores element's value and maintains the indexes for each element. HashMap stores elements key & value pair. For each value, there must be a key associated with HashMap.
ArrayList stores only a single object. HashMap stores elements in Key and value pairs.
We get the element by specifying the index of it in ArrayList. The elements are being fetched by the corresponding Key in HashMap.
The ArrayList maintains the order of the objects they are inserted. HashMap does not provide a guarantee of the order in which they are inserted.
ArrayList allows duplicate elements. HashMap allows duplicate values but does not allow duplicate keys.
The ArrayList always gives O(1) performance in best case or worst-case time complexity. The HashMap get() method has O(1) time complexity in the best case and O(n) time complexity in worst case.
ArrayList has any number of null elements. HashMap allows only one null Key and lots of null values.
ArrayList is the index-based data structure supported by the array. While HashMap is a mapped data structure that works on hashing to obtain stored values.

It is all about the differences between ArrayList and HashMap. They both are different from each other and work for a different purpose. If you are looking to store objects in Java, use HashMap if we need mapping keys to values and using ArrayList.

The similarity between ArrayList and HashMap:

  1. Both are not synchronized; We cannot use them in a multi-threading environment without any external synchronization.
  2. Both ArrayList and HashMap Iterator are fail-fast. It throws ConcurrentModificationException and detects the structural changes when the iterator is created in ArrayList or HashMap.
  3. Allows both ArrayList and HashMap null. HashMap allows null keys and values.
  4. Both uses the get() method. The ArrayList.get() method works based on the index and HashMap.get() method takes a parameter key_element of object type and refers to the key which associated value is fetched. Therefore, they both provides constant-time performance.
  5. An array supports ArrayList. Similarly, HashMap is also internally implemented by array.
  6. We use Iterator for accessing the elements of ArrayList and HashMap.

Next TopicJava Constant

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA