Saturday, 17 December 2016

Difference : ArrayList Vs HashMap

ArrayList and HashMap differ based on following parameters:

1.      Approach
ArrayList stores only the value of the element.
HashMap applies different mechanism as it stores value of the element along with a unique keyassociated to the stored value.

2.      Memory requirement
As ArrayList only stores the value of the element so it requires less memory space in comparision to HashMap.
On other hand HashMap needs to store a unique key for each value so memory requirement increases.

3.      Implementation
ArrayList implements List interface.
HashMap implements Map interface.

4.      Order maintainence
ArrayList ensures that the insertion order is maintained.
HashMap doesn’t guarantee of maintaining insertion order (if you care about order, use LinkedHashMap).
*In case where insertion order is maintained we get the elements returned in same order in which they were inserted while retrieving them.

5.      Null value
Any number of null values can be stored in ArrayList.
Only one null key is allowed in HashMap. But you can store any number of null values.

6.      Duplicate value
ArrayList accepts duplicate values while storing.
HashMap doesn’t accept duplicate Key. But duplicate values are allowed.

7.      Retrieve element
In ArrayList elements can be retrieved specifying index of the element.

No comments:

Post a Comment

If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.