aspose file tools*
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
Author

Hashmap

navat venu
Ranch Hand

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

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
Navat,

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.

Saket
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14103
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
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: http://aspose.com/file-tools
 
subject: Hashmap