Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

basic difference HashMap & HashTable

 
Suhas Madap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've a small doubt, what are the differences between the HashMap & HashTable?
In real time environment, why programmers will use HashTable?
 
Rachil Chandran
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic