• 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

K&B error ? Generics pg 632 Question 4

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it says

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

is wrong
when inserted in this

import java.util.*;
public class LongWordFinder {
public static void main(String[] args) {
String[] array = { "123", "12345678", "1", "12", "1234567890"};
List<String> list = Arrays.asList(array);
Collection<String> resultList = getLongWords(list);
}
// INSERT DECLARATION HERE
{
Collection<E> longWords = new ArrayList<E>();
for (E word : coll)
if (word.length() > 6) longWords.add(word);
return longWords;
}
}

I think this must be a mistake, cause it compiles fine for me.
I am pretty sure you can use wildcards in your return type for generics.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Peter,

The book is correct. You are right that you can use wildcards in return types for generics. However you cannot assign a Collection<? extends CharSequence> to a variable of type Collection<String>.

Let's assume your method has return type declared as Collection<? extends CharSequence>. Now you can return a value of type Collection<StringBuffer> because StringBuffer is a class implementing the CharSequence interface. The signature of the getLongWords() methods is happy with that. However you cannot assign a Collection<StringBuffer> to a variable of type List<String> since StringBuffer does not extend String (and you cannot extend String anyway b/c it is a final class).

Best Regards,
Ladislav
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I try to compile that, I get a really strange error message (the capture # seems to change - what on earth does it mean?):

---1st RUN
init:
deps-jar:
Compiling 1 source file to /home/ubuser/NetBeansProjects/GenericTest/build/classes
/home/ubuser/NetBeansProjects/GenericTest/src/LongWordFinder.java:7: incompatible types
found : java.util.Collection<capture#991 of ? extends java.lang.CharSequence>
required: java.util.Collection<java.lang.String>
Collection<String> resultList = getLongWords(list);
1 error
BUILD FAILED (total time: 0 seconds)

---2nd RUN

found : java.util.Collection<capture#978 of ? extends java.lang.CharSequence>
[ July 19, 2008: Message edited by: M Marsden ]
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic