5

Collections.copy method signature is,

public static <T> void copy(List<? super T> dest, List<? extends T> src)

Just wondering is it necessary for the dest type to be <? super T> and not just <T>?

public static void main(String[] args)
{
  simpleCopy(new ArrayList<Number>(), new ArrayList<Number>());
  simpleCopy(new ArrayList<Number>(), new ArrayList<Integer>());
}

public static <T> void simpleCopy(Collection<T> dest, Collection<?  extends T> src)
{
   for (T element : src)
     dest.add(element);
}
Prasanna
  • 2,390
  • 10
  • 11

0 Answers0