Refer to the 14 question below from K&B
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;
} }
Which declarations could be inserted at // INSERT DECLARATION HERE so that the program
will compile and run? (Choose all that apply.)
A. public static <E extends CharSequence> Collection<? extends CharSequence>
getLongWords(Collection<E> coll)
B. public static <E extends CharSequence> List<E>
getLongWords(Collection<E> coll)
C. public static Collection<E extends CharSequence> getLongWords(Collection<E> coll)
D. public static List<CharSequence>
getLongWords(Collection<CharSequence> coll)
E. public static List<? extends CharSequence>
getLongWords(Collection<? extends CharSequence> coll)
F. static public <E extends CharSequence> Collection<E>
getLongWords(Collection<E> coll)
G. static public <E super CharSequence> Collection<E>
getLongWords(Collection<E> coll)
Answer:F
I am not able to understand why answer A is not correct.
The return type from method getLongWords is Collection of type String
So as in option F we put a bounded option that is E extends CharSequence defining type E.
So why can't we use a wildcard extends CharSequence in the return type as in option A.