<? super E> and <? extends E> for List - java

Having the following simple class structure:
class A {
}
class B extends A {
}
class C extends B {
}
I'm creating an ArrayList to keep objects of the earlier created classes:
List<? extends A> list1 = new ArrayList<A>();
List<? extends B> list2 = new ArrayList<B>();
List<? extends C> list3 = new ArrayList<C>();
List<? super A> list4 = new ArrayList<A>();
List<? super B> list5 = new ArrayList<B>();
List<? super C> list6 = new ArrayList<C>();
To each of those lists I'm trying to add 1 object of each earlier created class: A,B,C. The only possible combination is:
adding object of class A,B,C to list4
adding object of class B and C to list5
adding object of class C to list list6.
The rest of the tries gives compiler errors, such us:
The method add(capture#1-of ? extends
A) in the type List is not applicable for the
arguments (A)
Why can't I add any object of class A,B,C to list1/2/3?
Why e.g. list4 accepts objects of classes A,B,C if they are supposed to be a super class of class A, as the list4 is defined?

"? extends A" means "some type derived from A (or A itself)". So for instance, a List<ByteArrayOutputStream> is compatible with List<? extends OutputStream> - but you shouldn't be able to add a FileOutputStream to such a list - it's meant to be a List<ByteArrayOutputStream>! All you know is that anything you fetch from the list will be an OutputStream of some kind.
"? super A" means "some type which is a superclass of A (or A itself)". So for instance, a List<OutputStream> is compatible with List<? super ByteArrayOutputStream>. You can definitely add a ByteArrayOutputStream to such a list - but if you fetch an item from the list, you can't really guarantee much about it.
See Angelika Langer's Generics FAQ for much more information.

The type definition List<? extends A> is not usable for a mutable List - the explanation given in Java generics Java Generics Pdf is
The add() method takes arguments of type E, the element type of the
collection.
When the actual type parameter is ?, it stands for some unknown type.
Any parameter
we pass to add would have to be a subtype of this unknown type. Since we
don’t know
what type that is, we cannot pass anything in.
However, when the typedef is List<? super A> then the type parameter ? is implicitly typed.

List<? extends A> list1
It is a list, whose type element could be any unknown subclass of A. For example, it could be a D subclass. Therefore, you can't add anything to it, it could be wrong...
List<? super A> list4
It is a list, whose type element could be A, or a superclass of A (does not exist in that case, except Object). Therefore, you can add A objects to it, or any subclass of A such as B or C.

It doesn't work that way.
You should use <? extends T> when you create function which argument is collection of unknown subtype of some type, and you want to fetch objects from that collection:
int sum(Collection<? extends Integer> collection) {
for (Integer : collection) {
// do sth
}
// ...
}
You cannot add new items to this collection, because you don't know which concrete type is this collection holding. All you know is that that type extends Integer.
You use <? super T> when you want to add new items of type T to collection and return that collection, but then you cannot guarantee what you can retrieve from it and you have to cast result of get() or check its type. You can safely add items of type T and subtypes, and retrieve items of type T.

List<? super B> allows you to use Lists of any supertype of B, i.e. list5 = new ArrayList<A>();
or list5 = new ArrayList<Object>();
You can safely add B (and subtypes) to every list that use supertypes of B, but you can not add any supertype of B. Imagine this:
public void myAdd(List<? super B> lst) {
lst.add(new Object()) //this is a supertype of B (compile time error)
}
...
ArrayList<A> list = new ArrayList<A>();
myAdd(list); //tries to add Object to a list of type A

Related

What is the point of stating that a type wildcard must extend Object? [duplicate]

I've seen the wildcard used before to mean any object - but recently saw a use of:
<? extends Object>
Since all objects extend Object, are these two usages synonymous?
<?> and <? extends Object> are synonymous, as you'd expect.
There are a few cases with generics where extends Object is not actually redundant. For example, <T extends Object & Foo> will cause T to become Object under erasure, whereas with <T extends Foo> it will become Foo under erasure. (This can matter if you're trying to retain compatibility with a pre-generics API that used Object.)
Source: http://download.oracle.com/javase/tutorial/extra/generics/convert.html; it explains why the JDK's java.util.Collections class has a method with this signature:
public static <T extends Object & Comparable<? super T>> T max(
Collection<? extends T> coll
)
Although <?> is supposed to be a shortcut for <? extend object>, there is a tiny difference between the two.
<?> is reifiable while <? extend object> is not. The reason they did this is to make it easier to distinguish reifiable type. Anything that looks like <? extends something>,<T>,<Integer> are nonreifiable.
For example, this code would work
List aList = new ArrayList<>();
boolean instanceTest = aList instanceof List<?>;
but this gives an error
List aList = new ArrayList<>();
boolean instancetest = aList instanceof List<? extends Object>;
for more info read Java generics and collections by Maurice Naftalin
<?> is a shorthand for <? extends Object>.
You may read below shared link for more details.
<?>
"?" denotes any unknown type, It can represent any Type at in code for. Use this wildcard if you are not sure about Type.
ArrayList<?> unknownList = new ArrayList<Number>(); //can accept of type Number
unknownList = new ArrayList<Float>(); //Float is of type Number
Note: <?> means anythings. So It can accept of Type which are not inherited from Object class.
<? extends Object>
<? extends Object> means you can pass an Object or a sub-class that extends Object class.
ArrayList<? extends Number> numberList = new ArrayList<Number>(); //Number of subclass
numberList = new ArrayList<Integer>(); //Integer extends Number
numberList = new ArrayList<Float>(); // Float extends Number
T – used to denote type
E – used to denote element
K – keys
V - values
N – for numbersRef:

Get from <? super A>

The whole idea of Generics is type-safety and preventing casts.
When i add() to a list that is <? super A>, the only way it lets me compile the get is to declare the variable type as Object.
ArrayList<? super A> list = new ArrayList<A>();
Object obj = list.get(0);
The fact is that I can only add A and A's sub-classes to this list.
So one, why doesnt it let me declare obj as type A?
A obj = list.get(0);
And two, the fact that type is now Object, it seems to loosen up the whole Generics idea - i am containing it in a umbrella type, and i need to cast the .get()
You define the list as a list of <? super A>, this means the list contains elements which are A, or any of it's superclasses, this causes a problem, because there can be any number of superclasses, which could go all the way up to object, so all the compiler can guarantee is that the item returned from the list is an object.
If you want to have a list that contains As you should define it as a List<A> which guarentees the list will contain only As or A's subclasses
If you have a variable of type List<? super A>, you can only add values of type A to it through that variable. That doesn't mean it can't have other objects in it:
List<Object> l = new ArrayList<>();
l.add(new Object());
List<? super A> l2 = l;
l2.add can only be called with values of type A, but l2.get could return non-A instances.

What is the difference between these two notations [duplicate]

This question already has answers here:
What is PECS (Producer Extends Consumer Super)?
(16 answers)
Closed 4 years ago.
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:
You can read a Number because any of the lists that could be assigned to foo3 contain a Number or a subclass of Number.
You can't read an Integer because foo3 could be pointing at a List<Double>.
You can't read a 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:
You can't add an Integer because foo3 could be pointing at a List<Double>.
You can't add a Double because foo3 could be pointing at a List<Integer>.
You can't add a 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:
You aren't guaranteed an Integer because foo3 could be pointing at a List<Number> or List<Object>.
You aren't guaranteed a Number because foo3 could be pointing at a List<Object>.
The only guarantee is that you will get an instance of an 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:
You can add an Integer because an Integer is allowed in any of above lists.
You can add an instance of a subclass of Integer because an instance of a subclass of Integer is allowed in any of the above lists.
You can't add a Double because foo3 could be pointing at an ArrayList<Integer>.
You can't add a Number because foo3 could be pointing at an ArrayList<Integer>.
You can't add an Object because foo3 could be pointing at an ArrayList<Integer>.
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<? extends Integer>. 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<? 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>.
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 <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
1. Extends
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):
C2: new ArrayList<C2>();, (an object that can store C2 or subtypes) or
D1: new ArrayList<D1>();, (an object that can store D1 or subtypes) or
D2: new 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:
you cannot list.add(new C2(){}); because it could be list = new ArrayList<D1>();
you cannot list.add(new D1(){}); because it could be list = new ArrayList<D2>();
and so on.
2. Super
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):
A1: new ArrayList<A1>();, (an object that can store A1 or subtypes) or
A2: new ArrayList<A2>();, (an object that can store A2 or subtypes) or
A3: new 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.
you can list.add(new C2(){}); because, regardless of the kind of List we're referencing, C2 is allowed
you can list.add(new D1(){}); because, regardless of the kind of List we're referencing, D1 is allowed
and so on. You probably noticed that these types correspond to the hierarchy starting from type C2.
Notes
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>
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:
you can always get Object from a list regardless of the wildcard.
you can always add null to a mutable list regardless of the wildcard.
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.
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:
When you get an item from List< ? extends X >, you can assign it to a variable of type X or any supertype of X, including Object.
When you get an item from List< ? super X >, you can only assign it to a variable of type 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
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,
1. Assignment to the list variable
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>();
2. Perform Read or Write operation on the list variable
`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.
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.
Using extends you can only get from the collection. You cannot put into it. Also, though super allows to both get and put, the return type during get is ? super T.
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 .
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.

Using addAll() on List<? extends E> a and b doesn't work

Why doesn't this compile?
List<? extends Number> a = new ArrayList<>();
List<? extends Number> b = new ArrayList<>();
a.addAll(b);
Because it wouldn't be safe.
List<? extends Number> should be read as some list where the element type extends Number. So in runtime a could be a List<Long> and b could be a List<BigInteger>. In that case, a.addAll(b) would mean "add all BigIntegers to the list of Longs" which, if allowed, obviously wouldn't be type safe.
https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/Iterables.html
What really worked for me was Guava:
com.google.common.collect.Iterables.concat(...)
List<? extends Number> means
Items in this List have all the same class. Not only do they extend
Number, but are all the same type.
Due to type erasure during compile, the byte code interpreter does not know (and cannot infer), whether the ? of a and the ? refer to the same class.

Generics lower bound ('super') issue? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java generics super keyword
I am not able to relate my knowledge with the below sample program. Please see the below sample program then my doubts are below that program.
import java.util.*;
class A { }
class B extends A { }
class C extends B { }
public class sampleprog {
public static void main(String[] args) {
List<? super A> list1 = new ArrayList<A>();
list1.add(new A());//valid. ok
list1.add(new B());//valid, why? it is not super to A?
list1.add(new C());//valid, why? it is not super to A?
List<? super B> list2 = new ArrayList<A>();
list2.add(new A());//not valid. why? it is also super to B!!!
list2.add(new B());
list2.add(new C());
List<? super C> list3 = new ArrayList<C>();
list3.add(new A());//not valid, why? It is super to A so should be valid!!!
list3.add(new B());//not valid, why? It is super to A so should be valid!!!
list3.add(new C());
}
}
My Doubts:
- As far as I know ? super T means any class you can add that is super to T but here output is different? Even subclass also added successfully that is totally confusing.
- output is not different with list initialization (List<? super C> list3 = new ArrayList<C>();) . In this initialization, I assigned list of A or B, output was same!
Please clear my doubts.
"? super T" means any class you can add that must be an instance of T or its sub class.
So List<? super C> list3 = new ArrayList<C>(); in the list3 either you can add instance of class C or its sub class instances (SomeClass extends C).
A parent object list can have its own, child and child of child but a List that has created for a child class can not have its parent object. Hope you are clear now.
List<? super B> means that
List<? super B> list1 = new ArrayList<A>();
List<? super B> list1 = new ArrayList<B>();
are allowed. ArrayList<A> is super of ArrayList<B>, isn't it? Can we add B's and C's to both ArrayList<A> and ArrayList<B>? Yes we can. Can we add A's? Only to ArrayList<A>. But List<? super B> is allowed to point to ArrayList<B> too. So we cannot add A's to List<? super B>.
If we use ? super in a generic declaration, the reference can point to same generic type of collection or super generic type of collection. So everything that passes a IS-A test is valid in this case.
Vote up #us2012's comment.
For List<? super A>
the type is A. A, B and C all subtype of A, so they can be added.
For List<? super B>
the type can be A, B. Both B and C are subtype of A or B.
Think about the difference between List<? super B> list2 = new ArrayList<A/B>()
For List<? super C>
the type can be A, B, C. So only C is subtype of A, B or C.
For example, Machine -> Car -> SportsCar. for List<? super Machine>, no matter Machine, Car and SportsCar, they are all Machine, so it's safe to be added.

Categories

Resources