File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between HashMap and arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between HashMap and arraylist" Watch "Difference between HashMap and arraylist" New topic

Difference between HashMap and arraylist

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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.

Thanks<br />Dinesh
Abhijit Das
Ranch Hand

Joined: Sep 25, 2007
Posts: 156
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.

Abhijit Das
SCJP 5.0 | SCWCD 1.5
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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

Joined: Aug 16, 2005
Posts: 15101

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).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
ben stone

Joined: Oct 04, 2007
Posts: 3
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

Joined: Aug 03, 2008
Posts: 9466

ArrayList is ordered while HashMap is not...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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.

I agree. Here's the link:
subject: Difference between HashMap and arraylist
It's not a secret anymore!