Using Java Dictionary...use a Hashtable?
Asked Answered
M

3

7

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!

Maintop answered 17/7, 2012 at 20:49 Comment(0)
G
2

The reason Hashtable is obsolete is because it was was replaced with Hashmap.

However, for the purposes of assigning labels to a setLabelTable, the "deficiencies" of Hashtable are not a problem.

Gaven answered 17/7, 2012 at 20:53 Comment(4)
After checking the Java docs of HashMap and HashTable, I can't see where it says that HashTable was replaced by HashMap (not even in the proposed link), instead HashMap has more advantages than using HashTable, also if you need a synchronized map you should use ConcurrentHashMap. If I'm wrong please enlighten me or then post that Java encourages to stop using HashTable.Dev
@LuiggiMendoza - it was not replaced "officially" as per Sun/Oracle; however the StackOveflow consensus is clearly that HashMap is the best "general" replacement.Gaven
Thanks, it's similar to what I've though, still I don't feel like I should say "HashMap is the replacement for HashTable", at least Sun/Oracle haven't made it official (yet).Dev
Sooo... what do we use instead of Dictionary to change JSlider label? I tried sending a HashMap<Integer, JLabel> as the argument and it obviously didn't work as the types are not compatible.Pipeline
I
4

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.

Insult answered 17/7, 2012 at 20:54 Comment(2)
alright, but why isn't there a setLabelTable implementation that takes a HashMap? Maybe my confusion is I don't understand what "obsolete" is supposed to me, because it nominally seems weird to preserve calls with "obsolete" types.Maintop
Java has strongly valued 100% backward compatibility over the years. Code written in 1995 is expected to compile and run against the latest runtimes, hence, sometimes you get weird stuff in the APIs. If they changed the method to take a java.util.Map, old JARs wouldn't run in the latest runtime anymore.Insult
G
2

The reason Hashtable is obsolete is because it was was replaced with Hashmap.

However, for the purposes of assigning labels to a setLabelTable, the "deficiencies" of Hashtable are not a problem.

Gaven answered 17/7, 2012 at 20:53 Comment(4)
After checking the Java docs of HashMap and HashTable, I can't see where it says that HashTable was replaced by HashMap (not even in the proposed link), instead HashMap has more advantages than using HashTable, also if you need a synchronized map you should use ConcurrentHashMap. If I'm wrong please enlighten me or then post that Java encourages to stop using HashTable.Dev
@LuiggiMendoza - it was not replaced "officially" as per Sun/Oracle; however the StackOveflow consensus is clearly that HashMap is the best "general" replacement.Gaven
Thanks, it's similar to what I've though, still I don't feel like I should say "HashMap is the replacement for HashTable", at least Sun/Oracle haven't made it official (yet).Dev
Sooo... what do we use instead of Dictionary to change JSlider label? I tried sending a HashMap<Integer, JLabel> as the argument and it obviously didn't work as the types are not compatible.Pipeline
S
0

Dictionary was "replaced" by Map and HashTable by a HashMap.
A HashTable is slow since it is synchronized so HashMap is the norm choice.
I am not sure why you worry that you use a "legacy" datastructure since if you must use a JSlider you have to use the HashMap.
Perhaps you should be wondering about which widgets to use instead? Just a thought...

Sempstress answered 17/7, 2012 at 21:4 Comment(1)
I'm not sure I understand your recommendation about considering which widget to use. I want to use a Slider. Are you suggesting there's an alternative in swing?Maintop

© 2022 - 2024 — McMap. All rights reserved.