• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Why HashTable does not support Generics

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reporting the source of the problem. Future seekers who come across this thread will be grateful.

Bill
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What a bizarre error! Thanks for the help...
 
Steve Page
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Seriously? That's what you're going with? I prefer this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic