• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why HashTable does not support Generics  RSS feed

 
Siamak Saarmann
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh? ... The Hashtable class has been updated to support Generics with Java 5. Or are you referring to a specific feature?

Henry
 
Siamak Saarmann
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Siamak Saarmann
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reporting the source of the problem. Future seekers who come across this thread will be grateful.

Bill
 
Steve Page
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a bizarre error! Thanks for the help...
 
Steve Page
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!