the difference between Super t> and extends t> in Java generics
Often found to have list<? Super t>, set<? Extends T> 's statement, what does it mean?
? Super T> represents the parent of any T, including T. Extends t> represents a subclass of any T, including T, below we analyze the specific differences between the two wildcard characters. extends
list<? The extends Number> Foo3 wildcard declaration means that the following assignment is valid:
Number "extends" number (in the context)
list<? extends number> Foo3 = new Arraylist<? extends number> ();
Integer extends number
list<. extends number> Foo3 = new Arraylist< extends Integer> ();
Double extends number
list< extends number> Foo3 = new Arraylist< extends Double> ();
Read operation by using the given assignment statement above, you must be able to read from the Foo3 list of the types of elements. You can read the number because the list above either contains the number element or the class element that contains number.
You cannot guarantee reading to integer because Foo3 may point to list<double>.
You cannot guarantee reading to double because Foo3 may point to list<integer>.
Write operations over the given assignment statement, you can legally insert a type of element into the Foo3.
You cannot insert an integer element because Foo3 may point to list<double>.
You cannot insert a double element because Foo3 may point to list<integer>.
You cannot insert a number element because Foo3 may point to list<integer>.
You can't go to list< Insert any type of object in the extends T>, because you cannot guarantee what type of object the list actually points to, and you cannot guarantee what type of objects are actually stored in the list. The only guarantee is that you can read a subclass of T or T from it. Super
Now consider list<? Super t>.
list<? The wildcard declaration of the super integer> Foo3 means that the following assignment is valid:
Integer is a "superclass" of the integer (in the context)
list< Super integer> Foo3 = new arraylist<integer& gt; ();
Number is a superclass of Integer
list<. Super integer> Foo3 = new arraylist<number> ();
Object is a superclass of Integer
list<. Super integer> Foo3 = new arraylist<object> ();
Read operation by using the given assignment statement above, you must be able to read from the Foo3 list of the types of elements. You cannot guarantee reading to integer because Foo3 may point to list<number> or list<object>.
You cannot guarantee reading to number because Foo3 may point to list<object>.
The only guarantee is that you can read objects (you don't know exactly what subclasses are) to object or object subclasses. Write operations you can legally insert a type of element into the Foo3 by using the given assignment statement above. You can insert an integer object because the list of above declarations supports integers.
You can insert the object of an integer subclass, because the integer subclass is also an integer, for the same reason.
You cannot insert a double object because Foo3 may point to arraylist<integer>.
You cannot insert a number object because Foo3 may point to arraylist<integer>.
You cannot insert object objects because Foo3 may point to arraylist<integer>.
PECS
Keep in mind the pecs Principle: Producers (Producer) use extends, and consumers (Consumer) use super.
Producers use extends
If you need a list that provides elements of type T (that is, you want to read from a list of elements of type T), you need to declare the list as Extends T>, such as list< Extends Integer>, so you can't add any elements to the list.
Consumers use Super
If you need a list that uses the elements of type T (that is, you want to add the elements of type T to the list), you need to declare the list. Super T>, such as list< Super Integer>, so you cannot guarantee the type of elements to be read from.
That is the producer, and also the consumer
If a list is to be produced and consumed, you cannot use generic wildcard declaration lists, such as list<integer>.
Example
Please refer to the Copy method in Java.util.Collections (JDK1.7):
/** * Copies all of the elements from one list into another. After the * operation, the index of each copied element in the destination list * is identical to its index In the source list. The destination * List must is at least as long as the source list. If It is longer, the * remaining elements in the destination list are unaffected.
<p> * * This is runs in linear time.
* * @param dest the destination list.
* @param src the source list.
* @throws indexoutofboundsexception If the destination list is too small * to contain the entire source list. * @throws unsupportedoperationexception If the destination list ' s * List-iterator does not support the
<tt>set</tt> operation. * * public static <T> void copy (LIST< Super t> dest, list< extends t> src) {int SRCSI
Ze = src.size (); if (Srcsize > Dest.size ()) throwNew Indexoutofboundsexception ("Source does not fit in dest"); if (Srcsize < Copy_threshold | |
(src instanceof randomaccess && dest instanceof randomaccess))
{for (int i=0; i<srcsize; i++) Dest.set (i, Src.get (i));
else {listiterator<? Super T> Di=dest.listiterator (); listiterator<?
Extends t> si=src.listiterator ();
for (int i=0; i<srcsize; i++) {di.next ();
Di.set (Si.next ()); }
}
}