Is there any conceptual difference between the two methods?
public static <T> void add1(final Collection<T> drivers, final Collection<? super T> persons) { persons.addAll(drivers); }
and
public static <T> void add2(final Collection<? extends T> drivers, final Collection<T> persons) { persons.addAll(drivers); }
The following main method compiles without any warnings and runs without any exceptions at runtime. And the result is the expected - 4.
public static void main(String[] args) { final Person person1 = new Person(); final Person person2 = new Person(); final Collection<Person> persons = new ArrayList<>(); persons.add(person1); persons.add(person2); final Driver driver1 = new Driver(); final Collection<Driver> drivers = new ArrayList<>(); drivers.add(driver1); add1(drivers, persons); add2(drivers, persons); System.out.println(persons.size()); }
I know the PECS principle, and since persons in the first method is a consumer, super should be used, respectively - extends should be used for drivers in the second method. But are there any mistakes? Any difference I can skip? If not, which version is preferred and why?
source share