19

When I tried to use the hashtable class, Netbeans gave me an error saying:

While still supported, these classes were made obsolete by the JDK1.2 collection classes, and should probably not be used in new development.

However, I can't seem to find an example online on a better replacement for Hashtable. Any suggestions?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Tony Stark
  • 3,353
  • 7
  • 26
  • 30

6 Answers6

36

The most direct replacement of a Hashtable is a HashMap.

One difference that could be important is that all relevant methods of Hashtable are synchronized while they are not synchronized on HashMap.

user2357112
  • 260,549
  • 28
  • 431
  • 505
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 4
    If you need the synchronization (provided by Hashtable), you can use [`Collections.synchronizedMap()`](http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedMap(java.util.Map)) to decorate a normal `HashMap` or use [`ConcurrentHashMap`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html) which allows concurrent reading and modifying without requiring locking. – Kasper van den Berg Feb 27 '14 at 16:04
  • But you'll be not be able to replace a Hashtable by a HashMap if you have a third tier library needing a HashMap or a Dictionary (super type of the HashMap) like the one in org.osgi.framework.BundleContext.registerService(String string, Object o, Dictionary dctnr) one. – javadev Jun 25 '14 at 08:47
7

A better replacement for Hashtable is HashMap.

As for being obsolete, I have no reference to it, but the Javadoc states:

As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework.

Hashtable is synchronized unlike HashMap.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
4

You could probably use Hashmap:

Map<String, Integer> mymap = new HashMap<String, Integer>();
foamdino
  • 360
  • 2
  • 5
4

You should use HashMap, but it is not designed for concurrent environments, where you may use ConcurrentHashMap.

OR

Map<K,V> myMap = Collections.synchronizedMap(/*any map instance*/);
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Anish Dasappan
  • 415
  • 2
  • 9
3

There are cases when a method outside your control requires an Hashtable. Java's own javax.management.ObjectName is such an example[1].

When the external interface requires it you have to use Hashtable. In netbeans, you can use:

@SupresssWarnings("UseOfObsoleteCollectionType")

to suppress the warning locally.

See

1: I wonder why they used Hashtable and not HashMap, since ObjectName is available since 1.5 and Hashtable is obsolete since 1.2

Community
  • 1
  • 1
Kasper van den Berg
  • 8,951
  • 4
  • 48
  • 70
2

Summary

The closest replacement is HashMap (usually via the Map interface).


But note that Hashtable is thread-safe while HashMap is not. This is not a problem in most cases and it was intentional to make most Java collections non-thread-safe to avoid performance penalty for most common scenarios. But if you relied on Hashtable thread-safety and now need a replacement that would also be thread-safe then you have two options:

  • If you need a simple synchronization wrapper for an existing Map - use Collections.synchronizedMap(...)
  • If you need a class carefully and optimally designed for concurrent access - use ConcurrentHashMap (usually via the ConcurrentMap interface that extends the Map interface with additional concurrency features).
Ruslan
  • 3,063
  • 1
  • 19
  • 28