• 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

What does this mean ?

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Error :
The method add(capture#1-of ? extends Object) in the type TreeSet<capture#1-of ? extends Object> is not applicable for the arguments (String)



Expected to add string Objects

thank you
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reference type of tsAlpha is TreeSet<? extends Object>. That means all the compiler knows about it is that it's a TreeSet with a specific type that extends Object. So it could be a TreeSet<String>, but it could also be a TreeSet<Integer>, TreeSet<Float>, etc.

The compiler will only let you call methods that it can guarantee will work. And it can't guarantee adding a String is safe, because it won't work with all the collections above.

It's a common mistake to think that Collection<? extends X> means that you can add anything to it that extends X. If you want that, just use Collection<X>.
 
Mark Yevgeny
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think they should add that feature to Compiler to auto-identify type of value
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun Giridhar wrote:I think they should add that feature to Compiler to auto-identify type of value


That would be impossible (or at least far more complicated than it's worth). The compiler is doing static analysis based on the reference type.

The real question is: if you want the compiler to know you can add strings, why don't you tell it? At the moment you're telling it that isn't safe. The only thing the compiler is doing wrong is believing what you told it, which is that that TreeSet could be a set of anything.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic