Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Java Dictionary...use a Hashtable?

I'm a bit surprised no one has asked about this specific case, cause it's kind of a weird inconsistency in the java standard libraries:

I'm using swing JSliders with custom labels; the only library call available to assign labels is: setLabelTable(Dictionary labels)

But Dictionary is an abstract class, and its only known subclass in the standard lib is Hashtable, which the api & various IDE's complain about because it's "obsolete."

The obvious thing to do is just use the Hashtable, but I'm wondering two things:

  1. Is there a better way to approach this?
  2. If Hashtable is the only usable class for this (in my opinion) reasonably important library call, on what basis is it "obsolete"?

Thanks!

like image 652
bcr Avatar asked Jul 17 '12 20:07

bcr


1 Answers

It is obsolete because it has been replaced with java.util.HashMap. The primary differences are that methods on HashTable are synchronized, and HashMap allows use of the null pointer as a key.

Modern versions of java have come a long way in the performance of un-contested synchronized operations, so there isn't really the performance concern that there used to be. (if you're running on up to date JDK on a major platform.) If an API requires a HashTable, go ahead and use it.

like image 176
Affe Avatar answered Sep 23 '22 14:09

Affe