File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes basic difference HashMap & HashTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "basic difference HashMap & HashTable" Watch "basic difference HashMap & HashTable" New topic
Author

basic difference HashMap & HashTable

Suhas Madap
Ranch Hand

Joined: Aug 29, 2006
Posts: 52
Hi,
I've a small doubt, what are the differences between the HashMap & HashTable?
In real time environment, why programmers will use HashTable?


Thank you,<br /> <br />Suhas K Madap<br /> <br />If you understand what you're doing, you're not learning anything."
Rachil Chandran
Ranch Hand

Joined: Mar 05, 2006
Posts: 67
A HashTable has synchronized methods while a HashMap does not.

A HashTable should be used if you are coding a multithreaded application, else a HashMap is a better choice as it does not suffer the performance load produced by synchronization.

Also HashTable does not allow null values for either the keys or the value in it while a HashMap allows multiple null values and one null key


SCJP2 96%
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Welcome to Java Ranch, Suhas.

Hashtable is considered a legacy Collection. When they implemented the JCF (Java Collections Framework) they retrofitted the old Collections (i.e. Vector, Stack and Hashtable) to conform to the new interaces ( List, Queue, Map). But the use of the old classes is discouraged now, and you are encouraged to use the new Collection classes.

One disadventage of Hashtable over HashMap is that first one is synchronized and, therefore, slower than the latter.

Even now, in order to make one of the new Collection classes synchronized you can use the Collections.synchronizedXxxxxx() method instead of using the legacy classes.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
HashMap is a class that actually exists in the standard Java libraries. HashMap is not - however Hashmap is. Capitalization is important in Java, and while it may seem silly, that's a fundamentally important concept in Java. Ignore it at your peril. The inconsistent naming of the Hashmap class is one of many reasons why that class should be avoided.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
One disadventage of Hashtable over HashMap is that first one is synchronized and, therefore, slower than the latter.

Meh - maybe. In most cases, using modern JDKs, this is remarkably irrelevant. Acquiring an uncontested lock is quite cheap - i.e. fast, and easy. Acquiring a contested lock is harder, but much, much less time-consuming than the alternative - if a lock is contested, that means that interference from multiple threads is significant, and that means that proper synchonization is vital to preserve the integrity of your data. Unfortunately most synchronized classes are not really thread-safe, as they require additional synchronization for safe use. Don't believe Sun's documentation when it refers to a class with synchronized methods as "thread-safe", as it's usually a dangerous fallacy.

If you need thread safety, you might as well use a HashMap, and apply additional synchronization using synchronized blocks as necessary Learning how to do this requires some study, but it's better than trying to navigate these hazards without proper knowledge. Hashtable is often a flawed solution; at worst, a dangerously misleading illusion of a solution. Because Hashtable can only guarantee that each individual method invocation is synchronized - it does nothing to ensure that there was no interference from another thread in between two Hashtable method calls. To do something like that, you need to know how to use a synchronized block outside the Hashtable class. Don't rely on a so-called "thread-safe" class to provide thread safety for you - if you're using multiple threads, you need to understand the issues well enough to provide proper synchronization yourself, using synchronized blocks. Trust no one.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Originally posted by Jim Yingst:
HashMap is a class that actually exists in the standard Java libraries. HashMap is not - however Hashmap is.

Jim, you're confusing the words - it's HashMap with capital M and Hashtable with lower case t. But ofcourse you already knew that...

The bottom line is: Forget about Hashtable, always use HashMap, and if you really require a synchronized Map, use Collections.synchronizedMap(...) to make your HashMap synchronized.

[ October 19, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Suhas Madap:

In real time environment


"Real time" does not mean "in actual practice". It refers to a special type of computing in which you must be able to guarantee the response time of a system. The phrase you are looking for is "real-world".

But this premise is wrong: in a real-world situtation, good programmers will use HashMap except in the very few cases where some API function requires a Hashtable as an argument.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Jesper]: Jim, you're confusing the words

Yep, the second mention of HashMap should have been HashTable - the thing that doesn't exist. Thanks for the catch.

[Jesper]: The bottom line is: Forget about Hashtable, always use HashMap

Agreed - aside from the [usually rare] need to interact with some backward external API that requires it.

[Jesper]: and if you really require a synchronized Map, use Collections.synchronizedMap(...) to make your HashMap synchronized.

That might work, but in many cases, you'll still need at least some external synchronization. Unless the map is never changed after initial setup, in which case you could just as well use Collections.unmodifiableMap().
[ October 19, 2006: Message edited by: Jim Yingst ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: basic difference HashMap & HashTable
 
Similar Threads
HashMap vs. Hashtable
Can't develop logic
Hashtables are synchronized...
Hashtable vs HashMap
HashMap or HashTable