Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacement for obsolete Hashtable class in Java

Tags:

java

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?

like image 912
Tony Stark Avatar asked Nov 22 '11 06:11

Tony Stark


People also ask

Is Hashtable still used in Java?

Ans. HashTable has been deprecated. As an alternative, ConcurrentHashMap has been provided. It uses multiple buckets to store data and hence much better performance than HashTable.

Should I use Hashtable or HashMap Java?

When to use HashMap and Hashtable? HashMap should be preferred over Hashtable for the non-threaded applications. In simple words , use HashMap in unsynchronized or single threaded applications . We should avoid using Hashtable, as the class is now obsolete in latest Jdk 1.8 .

What is Java Util Hashtable?

This class implements a hash table, which maps keys to values. Any non- null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.


2 Answers

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.

like image 118
Joachim Sauer Avatar answered Oct 18 '22 23:10

Joachim Sauer


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.

like image 20
Buhake Sindi Avatar answered Oct 18 '22 22:10

Buhake Sindi