Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What is the main difference between array and hashmap in java?


Asked by Whitley Salinas on Dec 05, 2021 Java



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.
Next,
Difference between ArrayList and HashMap in Java Interface Implemented: ArrayList implement List Interface while HashMap is an implementation of Map interface. Maintenance of the Insertion Order: ArrayList maintains the insertion order while HashMap does not maintain insertion order.
Just so, 1. HashMap is non synchronized. It is not-thread safe and can’t be shared between many threads without proper synchronization code whereas Hashtable is synchronized. It is thread-safe and can be shared with many threads. 2. HashMap allows one null key and multiple null values whereas Hashtable doesn’t allow any null key or value.
Similarly,
HashMap maps a value to a key, and the keys are stored in a set (so it can be in the set only once). HashMap doesn't preserve insertion order for the key set, while LinkedHashMap does retain insertion order for the keys.
In fact,
Both ArrayList and HashMap can be traversed through Iterator in Java. Both Somewhere use array, ArrayList is backed by array and HashMap is also internally implemented by Array