ArrayList vs HashMap

Difference between ArrayList and HashMap in Java

ArrayList HashMap
1) ArrayList implements List Interface . HashMap is an implementation of Map interface.
2)ArrayList only stores value or element. HashMap stores  key & value pairs.
3) ArrayList maintains insertion order of the elements. HashMap doesn’t  maintains the insertion order of the elements.
4)ArrayList can contain duplicate elements. HashMap doesn’t contain duplicate keys but contain duplicate values.
5) ArrayList get(index) method always gives an O(1) performance.

HashMap get(key)method can be O(1) in the best case and O(n) in the worst case.

6) ArrayList can have any number of null elements. HashMap allows one null key and multiple number of null values.
7) ArrayList is a collection of objects similar to Vector HashMap is a collection of key and value pairs similar to Hashtable.

 

Similarity between ArrayList and HashMap

1) Both ArrayList and HashMap are not synchronized, which means multiple threads can work on ArrayList and HashMap at the same time..

2) Both ArrayList and HashMap Iterator are fail-fast, they will throw ConcurrentModificationException .

3) Both ArrayList and HashMap allows null.
4) ArrayList contain duplicate elements and HashMap contain duplicate values.
5) Both ArrayList and HashMap can be traversed through Iterator .

Leave a comment