aspose file tools
The moose likes Java in General and the fly likes Why HashTable does not support Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Why HashTable does not support Generics" Watch "Why HashTable does not support Generics" New topic
Author

Why HashTable does not support Generics

Siamak Saarmann
Ranch Hand

Joined: Aug 21, 2004
Posts: 77
Hello,

Could someone please tell me what is the reason behind this? HashTable could at least use generics for the objects being stored in it.

Casting the objects (in HashTable.get method for example) ruins the code. It's a pity that in the middle of JSE1.5/1.6 we are forced to cast output objects of a collection.

Thank you


PhD Candidate: Distributed and Parallel Systems, Simulation and Modeling
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16687
    
  19

Huh? ... The Hashtable class has been updated to support Generics with Java 5. Or are you referring to a specific feature?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Siamak Saarmann
Ranch Hand

Joined: Aug 21, 2004
Posts: 77
I am now forced to use below code and the get method returns me "Object" which I need to cast to my own EnvRegion type.

private Hashtable hashEnvRegions = new Hashtable();

Now when I use :

private Hashtable<String,EnvRegion> hashEnvRegions = new Hashtable<String,EnvRegion>();

It gives this error:

The type HashTable is not generic, it cannot be parameterized with arguments <String,EnvRegion>.

Am I missing something? (I am using JSE1.6 for this project)

Mac
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
It's a bit suspicious when you keep talking about HashTable - there is no such class in the standard libraries. There is one called Hashtable though. You should have seen error messages about this. Have you created your own HashTable class?

The error message indicates your JDK knows what generics are, you it must be at least JDK 5. However the real Hashtable class certainly has been parameterized since JDK 5. I think you have a duplicate HashTable or Hashtable class somewhere. If you didn't create your own, check the classpath. Does it mention an older JDK for some reason?

Try this:

What do you get?
[ December 27, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Siamak Saarmann
Ranch Hand

Joined: Aug 21, 2004
Posts: 77
I am very sorry.

Eclipse had inserted below import command though java.util.* was present on my imports list:

import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;

Because eclipse folds the imports list I had not seen above strange import.

Sorry again.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12267
    
    1
Thanks for reporting the source of the problem. Future seekers who come across this thread will be grateful.

Bill
Steve Page
Greenhorn

Joined: Jul 17, 2008
Posts: 4
What a bizarre error! Thanks for the help...
Steve Page
Greenhorn

Joined: Jul 17, 2008
Posts: 4
OK, now I need help. When I run the suggested code, I get:

jar:file:/C:/Program%20Files/Java/jre1.6.0_06/lib/rt.jar!/com/sun/org/apache/xalan/internal/xsltc/runtime/Hashtable.class

Nevermind. I see that the default import is the problem, not the build path.

Thanks again for the resource suggestion.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32651
    
    4
Change the import to java.util.Hashtable.

Or better still, declare your object as a java.util.Map and then use java.util.HashMap.

What happens in Eclipse is that you enter the beginning of the name . . .Has . . . and push ctrl-space. Then you get a dropdown list of possible matches, and if you click on the xalan version (which may appear first) that's what you get. The options are in alphabetical order by class name and package name (I think), so com.something comes before java.something.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Why HashTable does not support Generics
 
Similar Threads
Generics
generics...
Generics
Generics <?>
Generics