Using the Java dictionary ... use a hashtable?

I'm a little surprised that no one asked about this particular case, because this is a strange inconsistency in the standard java libraries:

I use swing JSliders with custom shortcuts; the only library labels available are: setLabelTable(Dictionary labels)

But the dictionary is an abstract class, and its only known subclass in the standard library is Hashtable, which api and various IDEs complain about because they are "deprecated".

The obvious thing is to just use a Hashtable, but I am interested in two things:

  • Is there a better way to approach this?
  • If a Hashtable is the only useful class for this (in my opinion) sufficiently important library call, on what basis is it "deprecated"?

Thanks!

+6
source share
3 answers

The reason the Hashtable is deprecated is because it has been replaced by the Hashmap .

However, for labeling in setLabelTable, Hashtable "flaws" are not a problem.

+2
source

It is deprecated because it has been replaced with java.util.HashMap. The main differences are that the methods in HashTable are synchronized, and HashMap allows you to use a null pointer as a key.

Modern versions of java have come a long way in performing consistent, synchronized operations, so there really isn’t any performance issue that came before. (if you are using the updated JDK on the main platform.) If the API requires a HashTable, use it and use it.

+4
source

Dictionary been replaced with Map and HashTable with HashMap .
A HashTable slow because it is synchronized, so the HashMap is the norm choice.
I'm not sure why you are worried about using an "obsolete" data structure, because if you must use JSlider , you must use HashMap .
Perhaps you should consider which widgets to use instead. Just a thought ...

0
source

Source: https://habr.com/ru/post/920745/


All Articles