Replacement for obsolete Hashtable class in Java
Asked Answered
T

6

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?

Terra answered 22/11, 2011 at 6:59 Comment(1)
In general, you should use a HashMap.Priestcraft
C
38

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.

Cupronickel answered 22/11, 2011 at 7:2 Comment(2)
If you need the synchronization (provided by Hashtable), you can use Collections.synchronizedMap() to decorate a normal HashMap or use ConcurrentHashMap which allows concurrent reading and modifying without requiring locking.Rogelioroger
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<String, ?> dctnr) one.Knickerbocker
F
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.

Flunky answered 22/11, 2011 at 7:3 Comment(0)
F
4

You could probably use Hashmap:

Map<String, Integer> mymap = new HashMap<String, Integer>();
Friedlander answered 22/11, 2011 at 7:2 Comment(0)
L
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*/);
Loritalorn answered 22/11, 2011 at 7:8 Comment(0)
R
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

Rogelioroger answered 3/1, 2014 at 17:12 Comment(0)
I
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).
Influence answered 30/9, 2017 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.