File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hashmap" Watch "Hashmap" New topic


navat venu
Ranch Hand

Joined: May 14, 2006
Posts: 66
Could anyone explain in which scenario hashmap and hashtable will be used?
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229

HashTable and HashMap are data structures providing key-value access to data. Following are the basic differences between the two are:
1. HashTable provides default synchronization whereas HashMap does not.
2. Hashtable does not permit null values whereas HashMap does.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

Are you asking what HashMap and Hashtable are, or do you already know that and do you want to know when you should use one or the other?

HashMap and Hashtable are both associative array data structures.

Hashtable is a legacy collection class. You shouldn't use it anymore; instead, you should always use HashMap. If you really need a synchronized map, you should use the method java.util.Collections.synchronizedMap(...), for example:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hashtable should be used in the past, HashMap in the future.

Be darned sure you know what a synchronized map does before you count on it for thread safety. It's very likely you'll want to synchronize all the code that accesses the map instead of using a synchronized map.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Hashmap
It's not a secret anymore!