• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics error, can not understand

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have the follwoing code. It is giving error : The method put(capture#3-of ?, capture#4-of ?) in the type Hashtable<capture#3-of ?,capture#4-of ?> is not applicable for the arguments (Object, Test)




If I cast it will show the warning. Can anybody tell me the reason.
Please help.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
theInclusion is a Hashtable<Object,Hashtable<?,?>>. The values are instances Hashtable<?,?>. The key and value type are no longer known. That means theInclusion.get(obj) could (as far as the compiler is concerned) return a Hashtable<Integer,String>. That would cause problems later on if you then add a Test instance.

You can solve this by changing the signature of theInclusion:
Even better is declaring against interfaces (search around for why):
Since Hashtable implements Map the rest of the code can stay as it is. If you want to use a non-synchronized HashMap at a later stage you only need to change one line - the initialization.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic