What is the difference between List<? super T>
and List<? extends T>
?
I used to use List<? extends T>
, but it does not allow me to add elements to it list.add(e)
, whereas the List<? super T>
does.
What is the difference between List<? super T>
and List<? extends T>
?
I used to use List<? extends T>
, but it does not allow me to add elements to it list.add(e)
, whereas the List<? super T>
does.
extends
The wildcard declaration of List<? extends Number> foo3
means that any of these are legal assignments:
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 Number
Reading - Given the above possible assignments, what type of object are you guaranteed to read from List foo3
:
Number
because any of the lists that could be assigned to foo3
contain a Number
or a subclass of Number
.Integer
because foo3
could be pointing at a List<Double>
.Double
because foo3
could be pointing at a List<Integer>
.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:
Integer
because foo3
could be pointing at a List<Double>
.Double
because foo3
could be pointing at a List<Integer>
.Number
because foo3
could be pointing at a List<Integer>
.You can't add any object to List<? extends T>
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
.
super
Now consider List <? super T>
.
The wildcard declaration of List<? super Integer> foo3
means that any of these are legal assignments:
List<? 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 Integer
Reading - Given the above possible assignments, what type of object are you guaranteed to receive when you read from List foo3
:
Integer
because foo3
could be pointing at a List<Number>
or List<Object>
.Number
because foo3
could be pointing at a List<Object>
.Object
or subclass of Object
(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:
Integer
because an Integer
is allowed in any of above lists.Integer
because an instance of a subclass of Integer
is allowed in any of the above lists.Double
because foo3
could be pointing at an ArrayList<Integer>
.Number
because foo3
could be pointing at an ArrayList<Integer>
.Object
because foo3
could be pointing at an ArrayList<Integer>
.Remember PECS: "Producer Extends, Consumer Super".
"Producer Extends" - If you need a List
to produce T
values (you want to read T
s from the list), you need to declare it with ? extends T
, e.g. List<? extends Integer>
. But you cannot add to this list.
"Consumer Super" - If you need a List
to consume T
values (you want to write T
s into the list), you need to declare it with ? super T
, e.g. List<? super Integer>
. 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<Integer>
.
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 <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));
}
}
Also see How can I add to List<? extends Number> data structures?
Imagine having this hierarchy
By writing
List<? extends C2> list;
you are saying that list
will be able to reference an object of type (for example) ArrayList
whose generic type is one of the 7 subtypes of C2
(C2
included):
new ArrayList<C2>();
, (an object that can store C2 or subtypes) ornew ArrayList<D1>();
, (an object that can store D1 or subtypes) ornew ArrayList<D2>();
, (an object that can store D2 or subtypes) or...and so on. Seven different cases:
1) new ArrayList<C2>(): can store C2 D1 D2 E1 E2 E3 E4
2) new ArrayList<D1>(): can store D1 E1 E2
3) new ArrayList<D2>(): can store D2 E3 E4
4) new ArrayList<E1>(): can store E1
5) new ArrayList<E2>(): can store E2
6) new ArrayList<E3>(): can store E3
7) new ArrayList<E4>(): can store E4
We have a set of "storable" types for each possible case: 7 (red) sets here graphically represented
As you can see, there is not a safe type that is common to every case:
list.add(new C2(){});
because it could be list = new ArrayList<D1>();
list.add(new D1(){});
because it could be list = new ArrayList<D2>();
and so on.
By writing
List<? super C2> list;
you are saying that list
will be able to reference an object of type (for example) ArrayList
whose generic type is one of the 7 supertypes of C2
(C2
included):
new ArrayList<A1>();
, (an object that can store A1 or subtypes) ornew ArrayList<A2>();
, (an object that can store A2 or subtypes) ornew ArrayList<A3>();
, (an object that can store A3 or subtypes) or...and so on. Seven different cases:
1) new ArrayList<A1>(): can store A1 B1 B2 C1 C2 D1 D2 E1 E2 E3 E4
2) new ArrayList<A2>(): can store A2 B2 C1 C2 D1 D2 E1 E2 E3 E4
3) new ArrayList<A3>(): can store A3 B3 C2 C3 D1 D2 E1 E2 E3 E4
4) new ArrayList<A4>(): can store A4 B3 B4 C2 C3 D1 D2 E1 E2 E3 E4
5) new ArrayList<B2>(): can store B2 C1 C2 D1 D2 E1 E2 E3 E4
6) new ArrayList<B3>(): can store B3 C2 C3 D1 D2 E1 E2 E3 E4
7) new ArrayList<C2>(): can store C2 D1 D2 E1 E2 E3 E4
We have a set of "storable" types for each possible case: 7 (red) sets here graphically represented
As you can see, here we have seven safe types that are common to every case: C2
, D1
, D2
, E1
, E2
, E3
, E4
.
list.add(new C2(){});
because, regardless of the kind of List we're referencing, C2
is allowedlist.add(new D1(){});
because, regardless of the kind of List we're referencing, D1
is allowedand so on. You probably noticed that these types correspond to the hierarchy starting from type C2
.
Here the complete hierarchy if you wish to make some tests
interface A1{}
interface A2{}
interface A3{}
interface A4{}
interface B1 extends A1{}
interface B2 extends A1,A2{}
interface B3 extends A3,A4{}
interface B4 extends A4{}
interface C1 extends B2{}
interface C2 extends B2,B3{}
interface C3 extends B3{}
interface D1 extends C1,C2{}
interface D2 extends C2{}
interface E1 extends D1{}
interface E2 extends D1{}
interface E3 extends D2{}
interface E4 extends D2{}
I love the answer from @Bert F but this is the way my brain sees it.
I have an X in my hand. If I want to write my X into a List, that List needs to be either a List of X or a List of things that my X can be upcast to as I write them in i.e. any superclass of X...
List<? super X>
If I get a List and I want to read an X out of that List, that better be a List of X or a List of things that can be upcast to X as I read them out, i.e. anything that extends X
List<? extends X>
Hope this helps.
Based on Bert F's answer I would like to explain my understanding.
Lets say we have 3 classes as
public class Fruit{}
public class Melon extends Fruit{}
public class WaterMelon extends Melon{}
Here We have
List<? extends Fruit> fruitExtendedList = …
//Says that I can be a list of any object as long as this object extends Fruit.
Ok now lets try to get some value from fruitExtendedList
Fruit fruit = fruitExtendedList.get(position)
//This is valid as it can only return Fruit or its subclass.
Again lets try
Melon melon = fruitExtendedList.get(position)
//This is not valid because fruitExtendedList can be a list of Fruit only, it may not be
//list of Melon or WaterMelon and in java we cannot assign sub class object to
//super class object reference without explicitly casting it.
Same is the case for
WaterMelon waterMelon = fruitExtendedList.get(position)
Now lets try to set some object in fruitExtendedList
Adding fruit object
fruitExtendedList.add(new Fruit())
//This in not valid because as we know fruitExtendedList can be a list of any
//object as long as this object extends Fruit. So what if it was the list of
//WaterMelon or Melon you cannot add Fruit to the list of WaterMelon or Melon.
Adding Melon object
fruitExtendedList.add(new Melon())
//This would be valid if fruitExtendedList was the list of Fruit but it may
//not be, as it can also be the list of WaterMelon object. So, we see an invalid
//condition already.
Finally let try to add WaterMelon object
fruitExtendedList.add(new WaterMelon())
//Ok, we got it now we can finally write to fruitExtendedList as WaterMelon
//can be added to the list of Fruit or Melon as any superclass reference can point
//to its subclass object.
But wait what if someone decides to make a new type of Lemon lets say for arguments sake SaltyLemon as
public class SaltyLemon extends Lemon{}
Now fruitExtendedList can be list of Fruit, Melon, WaterMelon or SaltyLemon.
So, our statement
fruitExtendedList.add(new WaterMelon())
is not valid either.
Basically we can say that we cannot write anything to a fruitExtendedList.
This sums up List<? extends Fruit>
Now lets see
List<? super Melon> melonSuperList= …
//Says that I can be a list of anything as long as its object has super class of Melon.
Now lets try to get some value from melonSuperList
Fruit fruit = melonSuperList.get(position)
//This is not valid as melonSuperList can be a list of Object as in java all
//the object extends from Object class. So, Object can be super class of Melon and
//melonSuperList can be a list of Object type
Similarly Melon, WaterMelon or any other object cannot be read.
But note that we can read Object type instances
Object myObject = melonSuperList.get(position)
//This is valid because Object cannot have any super class and above statement
//can return only Fruit, Melon, WaterMelon or Object they all can be referenced by
//Object type reference.
Now, lets try to set some value from melonSuperList.
Adding Object type object
melonSuperList.add(new Object())
//This is not valid as melonSuperList can be a list of Fruit or Melon.
//Note that Melon itself can be considered as super class of Melon.
Adding Fruit type object
melonSuperList.add(new Fruit())
//This is also not valid as melonSuperList can be list of Melon
Adding Melon type object
melonSuperList.add(new Melon())
//This is valid because melonSuperList can be list of Object, Fruit or Melon and in
//this entire list we can add Melon type object.
Adding WaterMelon type object
melonSuperList.add(new WaterMelon())
//This is also valid because of same reason as adding Melon
To sum it up we can add Melon or its subclass in melonSuperList and read only Object type object.
I'd like to visualize the difference. Suppose we have:
class A { }
class B extends A { }
class C extends B { }
List<? extends T>
- reading and assigning:
|-------------------------|-------------------|---------------------------------|
| wildcard | get | assign |
|-------------------------|-------------------|---------------------------------|
| List<? extends C> | A B C | List<C> |
|-------------------------|-------------------|---------------------------------|
| List<? extends B> | A B | List<B> List<C> |
|-------------------------|-------------------|---------------------------------|
| List<? extends A> | A | List<A> List<B> List<C> |
|-------------------------|-------------------|---------------------------------|
List<? super T>
- writing and assigning:
|-------------------------|-------------------|-------------------------------------------|
| wildcard | add | assign |
|-------------------------|-------------------|-------------------------------------------|
| List<? super C> | C | List<Object> List<A> List<B> List<C> |
|-------------------------|-------------------|-------------------------------------------|
| List<? super B> | B C | List<Object> List<A> List<B> |
|-------------------------|-------------------|-------------------------------------------|
| List<? super A> | A B C | List<Object> List<A> |
|-------------------------|-------------------|-------------------------------------------|
In all of the cases:
Object
from a list regardless of the wildcard.null
to a mutable list regardless of the wildcard.super is a lower bound, and extends is an upper bound.
According to http://download.oracle.com/javase/tutorial/extra/generics/morefun.html :
The solution is to use a form of bounded wildcard we haven't seen yet: wildcards with a lower bound. The syntax ? super T denotes an unknown type that is a supertype of T (or T itself; remember that the supertype relation is reflexive). It is the dual of the bounded wildcards we've been using, where we use ? extends T to denote an unknown type that is a subtype of T.
Adding an item to the list:
List< ? extends X > doesn't allow to add anything, except for null
into the list.
List< ? super X > allows to add anything that is-a X (X or its subtype), or null.
Getting an item from the list:
Object
.Some examples:
List<? extends Number> list1 = new ArrayList<Integer>();
list1.add(null); //OK
Number n = list1.get(0); //OK
Serializable s = list1.get(0); //OK
Object o = list1.get(0); //OK
list1.add(2.3); //ERROR
list1.add(5); //ERROR
list1.add(new Object()); //ERROR
Integer i = list1.get(0); //ERROR
List<? super Number> list2 = new ArrayList<Number>();
list2.add(null); //OK
list2.add(2.3); //OK
list2.add(5); //OK
Object o = list2.get(0); //OK
list2.add(new Object()); //ERROR
Number n = list2.get(0); //ERROR
Serializable s = list2.get(0); //ERROR
Integer i = list2.get(0); //ERROR
You can go through all the answers above to understand why the .add()
is restricted to '<?>'
, '<? extends>'
, and partly to '<? super>'
.
But here's the conclusion of it all if you want to remember it, and dont want to go exploring the answer every time:
List<? extends A>
means this will accept any List
of A
and subclass of A
.
But you cannot add anything to this list. Not even objects of type A
.
List<? super A>
means this will accept any list of A
and superclass of A
.
You can add objects of type A
and its subclasses.
The most confusing thing here is that whatever type restrictions we specify, assignment works only one way:
baseClassInstance = derivedClassInstance;
You may think that Integer extends Number
and that an Integer
would do as a <? extends Number>
, but the compiler will tell you that <? extends Number> cannot be converted to Integer
(that is, in human parlance, it is wrong that anything that extends number can be converted to Integer):
class Holder<T> {
T v;
T get() { return v; }
void set(T n) { v=n; }
}
class A {
public static void main(String[]args) {
Holder<? extends Number> he = new Holder();
Holder<? super Number> hs = new Holder();
Integer i;
Number n;
Object o;
// Producer Super: always gives an error except
// when consumer expects just Object
i = hs.get(); // <? super Number> cannot be converted to Integer
n = hs.get(); // <? super Number> cannot be converted to Number
// <? super Number> cannot be converted to ... (but
// there is no class between Number and Object)
o = hs.get();
// Consumer Super
hs.set(i);
hs.set(n);
hs.set(o); // Object cannot be converted to <? super Number>
// Producer Extends
i = he.get(); // <? extends Number> cannot be converted to Integer
n = he.get();
o = he.get();
// Consumer Extends: always gives an error
he.set(i); // Integer cannot be converted to <? extends Number>
he.set(n); // Number cannot be converted to <? extends Number>
he.set(o); // Object cannot be converted to <? extends Number>
}
}
hs.set(i);
is ok because Integer
can be converted to any superclass of Number
(and not because Integer
is a superclass of Number
, which is not true).
EDIT added a comment about Consumer Extends and Producer Super -- they are not meaningful because they specify, correspondingly, nothing and just Object
. You are advised to remember PECS because CEPS is never useful.
The up voted answers covers the details on many aspects. However, I would try to answer this in different way.
There are 2 things we need to consider,
List<? extends X> listvar;
Here, any list of X or list of subclasses of X can be assigned to listvar.
List<? extends Number> listvar;
listvar = new ArrayList<Number>();
listvar = new ArrayList<Integer>();
List<? super X> listvar;
Here, any list of X or list of superclasses of X can be assigned to listvar.
List<? super Number> listvar;
listvar = new ArrayList<Number>();
listvar = new ArrayList<Object>();
`List<? extends X> listvar;`
You can use this feature to accept a list in method arguments and perform any operations on type X (Note: You can only read objects of type X from the list).
`List<? super Number> listvar;
You can use this feature to accept a list in method arguments and perform any operations on type Object as You can only read objects of type Object from the list. But yes, additional thing here is , you can add objects of type X into the list.
The generic wildcards target two primary needs:
Reading from a generic collection Inserting into a generic collection There are three ways to define a collection (variable) using generic wildcards. These are:
List<?> listUknown = new ArrayList<A>();
List<? extends A> listUknown = new ArrayList<A>();
List<? super A> listUknown = new ArrayList<A>();
List<?>
means a list typed to an unknown type. This could be a List<A>
, a List<B>
, a List<String>
etc.
List<? extends A>
means a List of objects that are instances of the class A
, or subclasses of A
(e.g. B and C).
List<? super A>
means that the list is typed to either the A class
, or a superclass of A
.
Read more : http://tutorials.jenkov.com/java-generics/wildcards.html
When to use extends and super
Wildcards are most useful in method parameters. They allow for the necessary flexibility in method interfaces.
People are often confused when to use extends and when to use super bounds. The rule of thumb is the get-put principle. If you get something from a parametrized container, use extends.
int totalFuel(List<? extends Vehicle> list) {
int total = 0;
for(Vehicle v : list) {
total += v.getFuel();
}
return total;}
The method totalFuel gets Vehicles from the list, asks them about how much fuel they have, and computes the total. If you put objects into a parameterized container, use super.
int totalValue(Valuer<? super Vehicle> valuer) {
int total = 0;
for(Vehicle v : vehicles) {
total += valuer.evaluate(v);
}
return total;}
The method totalValue puts Vehicles into the Valuer. It's useful to know that extends bound is much more common than super.
Example, Order of inheritance is assumed as O > S > T > U > V
Using extends Keyword ,
Correct:
List<? extends T> Object = new List<T>();
List<? extends T> Object = new List<U>();
List<? extends T> Object = new List<V>();
InCorrect:
List<? extends T> Object = new List<S>();
List<? extends T> Object = new List<O>();
super Keyword:
Correct:
List<? super T> Object = new List<T>();
List<? super T> Object = new List<S>();
List<? super T> Object = new List<O>();
InCorrect:
List<? super T> Object = new List<U>();
List<? super T> Object = new List<V>();
Adding object: List Object = new List();
Object.add(new T()); //error
But Why error ? Let's look at the Possibilities of initializations of List Object
List<? extends T> Object = new List<T>();
List<? extends T> Object = new List<U>();
List<? extends T> Object = new List<V>();
If we use Object.add(new T()); then it will be correct only if
List<? extends T> Object = new List<T>();
But there are extra two possibilities
List Object = new List(); List Object = new List(); If we try to add (new T()) to the above two possibilities it will give an error because T is the superior class of U and V . we try to add a T object [which is (new T()) ] to List of type U and V . Higher class object(Base class) cannot be passed to lower class Object(Sub class).
Due to the extra two possibilities , Java gives you error even if you use the correct possilibity as Java don't know what Object you are referring to .So you can't add objects to List Object = new List(); as there are possibilities that are not valid.
Adding object: List Object = new List();
Object.add(new T()); // compiles fine without error
Object.add(new U()); // compiles fine without error
Object.add(new V()); // compiles fine without error
Object.add(new S()); // error
Object.add(new O()); // error
But why error occurs in the above two ? we can use Object.add(new T()); only on the below possibilities,
List<? super T> Object = new List<T>();
List<? super T> Object = new List<S>();
List<? super T> Object = new List<O>();
If we Tried to use Object.add(new T()) in List Object = new List(); and List Object = new List(); then it will give error This is because We can't add T object[which is new T()] to the List Object = new List(); because it is an object of type U . We can't add a T object[which is new T()] to U Object because T is a base class and U is a sub class . We can't add base class to subclass and that's why error occurs . This is same for the another case .