Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between HashMap and arraylist

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers,

Bit confused between differences of HashMap and ArrayList.
I know both are unsynchronized and are much faster than Hastable and Vector.
Can you please expalin me the exact difference between the two.

 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMap is a collection of key and value pairs similar to Hashtable. But , it supports null and one duplicate key
ArrayList is a collection of objects similar to Vector. It is growable array.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.
But can you expalin in depth on " it supports null and one duplicate key". It would be great you can expalin me iwth example to get clear picture.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maps do not support duplicate keys, so HashMap also does not support duplicate keys. So "it supports one duplicate key" is wrong.

"It supports null" means that you can use null as a key or value. Hashtable does not support null for keys or values (you'll get a NullPointerException if you try to use null as a key or value in a Hashtable).
 
ben stone
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know what are the similarities between ArrayList and HashMap, I was asked in an interview this questions. I mean I read somewhere that both can be used for faster iteration and random access. I would like to know if there are more similarities out there between ArrayList and HashMap.
 
Ankit Garg
Sheriff
Posts: 9512
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList is ordered while HashMap is not...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both are not sorted.
Both are not synchronized.
Both are since java 1.2.


HashMap contain (key, Value) pair.
ArrayList is a Collection, HashMap is not a Collection.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic