gotchajavaCritical
Difference between <? super T> and <? extends T> in Java
Viewed 0 times
javaextendsandsuperbetweendifference
Problem
What is the difference between
I used to use
List and List ?I used to use
List, but it does not allow me to add elements to it list.add(e), whereas the List does.Solution
extendsThe wildcard declaration of
List foo3 means that any of these are legal assignments:List foo3 = new ArrayList(); // Number "extends" Number (in this context)
List foo3 = new ArrayList(); // Integer extends Number
List foo3 = new ArrayList(); // Double extends Number-
Reading - Given the above possible assignments, what type of object are you guaranteed to read from
List foo3:- You can read a
Numberbecause any of the lists that could be assigned tofoo3contain aNumberor a subclass ofNumber.
- You can't read an
Integerbecausefoo3could be pointing at aList.
- You can't read a
Doublebecausefoo3could be pointing at aList.
-
Writing - Given the above possible assignments, what type of object could you add to
List foo3 that would be legal for all the above possible ArrayList assignments:- You can't add an
Integerbecausefoo3could be pointing at aList.
- You can't add a
Doublebecausefoo3could be pointing at aList.
- You can't add a
Numberbecausefoo3could be pointing at aList.
You can't add any object to
List because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object is allowed in that List. The only "guarantee" is that you can only read from it and you'll get a T or subclass of T.superNow consider
List .The wildcard declaration of
List foo3 means that any of these are legal assignments:List foo3 = new ArrayList(); // Integer is a "superclass" of Integer (in this context)
List foo3 = new ArrayList(); // Number is a superclass of Integer
List foo3 = new ArrayList(); // Object is a superclass of Integer-
Reading - Given the above possible assignments, what type of object are you guaranteed to receive when you read from
List foo3:- You aren't guaranteed an
Integerbecausefoo3could be pointing at aListorList.
- You aren't guaranteed a
Numberbecausefoo3could be pointing at aList.
- The only guarantee is that you will get an instance of an
Objector subclass ofObject(but you don't know what subclass).
-
Writing - Given the above possible assignments, what type of object could you add to
List foo3 that would be legal for all the above possible ArrayList assignments:- You can add an
Integerbecause anIntegeris allowed in any of above lists.
- You can add an instance of a subclass of
Integerbecause an instance of a subclass ofIntegeris allowed in any of the above lists.
- You can't add a
Doublebecausefoo3could be pointing at anArrayList.
- You can't add a
Numberbecausefoo3could be pointing at anArrayList.
- You can't add an
Objectbecausefoo3could be pointing at anArrayList.
PECS
Remember PECS: "Producer Extends, Consumer Super".
-
"Producer Extends" - If you need a
List to produce T values (you want to read Ts from the list), you need to declare it with ? extends T, e.g. List. But you cannot add to this list.-
"Consumer Super" - If you need a
List to consume T values (you want to write Ts into the list), you need to declare it with ? super T, e.g. List. But there are no guarantees what type of object you may read from this list.-
If you need to both read from and write to a list, you need to declare it exactly with no wildcards, e.g.
List.Example
Note this example from the Java Generics FAQ. Note how the source list
src (the producing list) uses extends, and the destination list dest (the consuming list) uses super:public class Collections {
public static void copy(List dest, List src) {
for (int i = 0; i < src.size(); i++)
dest.set(i, src.get(i));
}
}Also see
How can I add to List data structures?
Code Snippets
List<? extends Number> foo3 = new ArrayList<Number>(); // Number "extends" Number (in this context)
List<? extends Number> foo3 = new ArrayList<Integer>(); // Integer extends Number
List<? extends Number> foo3 = new ArrayList<Double>(); // Double extends NumberList<? super Integer> foo3 = new ArrayList<Integer>(); // Integer is a "superclass" of Integer (in this context)
List<? super Integer> foo3 = new ArrayList<Number>(); // Number is a superclass of Integer
List<? super Integer> foo3 = new ArrayList<Object>(); // Object is a superclass of Integerpublic class Collections {
public static <T> void copy(List<? super T> dest, List<? extends T> src) {
for (int i = 0; i < src.size(); i++)
dest.set(i, src.get(i));
}
}Context
Stack Overflow Q#4343202, score: 2091
Revisions (0)
No revisions yet.