Articles

Difference between arraylist and hashmap

Difference between arraylist and hashmap


Here we are going to learn difference between arraylist and hashmap or arraylist vs hashmap with example.
 
ArrayList vs 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.
  • In HashMap elements can be retrieved by specifying its Key.

 

 


Java Interview Questions

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Sonu Pandit
Technology geek, loves to write and share knowledge with the world. Having 10+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :    Published Date : Jun 28,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!