aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between HashMap and arraylist" Watch "Difference between HashMap and arraylist" New topic
Author

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: 14150
    
  18

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 7 API documentation
Scala Notes - My blog about Scala
ben stone
Greenhorn

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
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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.


SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between HashMap and arraylist