• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Difference between HashMap and arraylist

 
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.

 
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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).
 
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.
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList is ordered while HashMap is not...
 
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.
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!