• 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

Question about generics

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question about generics...the code is adapted from K&B

I have the following code which compiles:



But as soon as I call the function with


the code won't compile anymore.
The book explains :

The return value is too vague. The last line of the method expects the return value to be Collection<String>, not Collection<? extends CharSequence>



But what is the difference between


I would assume that <E extends CharSequence> Collection<E> is equal to <E extends CharSequence> Collection<? extends CharSequence>

Can anyone please explain the difference? Thanks in advance!
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you call the method with

List<String> ll = ...;

Collection<String> resultList = getLongWords(ll);

Then as per the contract of the method which is

<E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)

the method can return any Collection which contains elements of any sub-type of CharSequence. String is a sub-type of CharSequence but the method says that it can return a Collection of any sub-type of CharSequence. So you cannot assume that it will return a collection of Strings.

But if you change the return type of the method to


<E extends CharSequence> Collection<E> getLongWords(Collection<E> coll)

Then calling the method with

List<String> ll = ...;

Collection<String> resultList = getLongWords(ll);

This time the compiler knows that the return type of the method is E. And since in this invocation E is String (as you passing List<String> , so it will allow you to assign the returning value from the method to Collection<String>.
 
Sylvia Trommer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aaah, I get it, thanks a lot!!
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeh!!! I solved a Generics Problem!!!
 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So now you can give SCJP 1.6 and not crib about 1.4 or 1.6
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic