Generics? Super T and? Extends T difference between generics? Super T and the difference? Extends T's

Original Source:  Concurrent Programming Network

Often found List <? Super T>, Set <? Extends T> statement, what does it mean? <? Super T> T represents the parent of any class, including T, including, <? Extends T> represents a subclass of T, including any T account, we analyze in detail the following two wildcard specific difference.

 

extends

<? Extends Number> List foo3 wildcard declaration means the following assignment is legal:

01 // Number "extends" Number (in this context)
02  
03 List<? extends Number> foo3 = new ArrayList<? extends Number>();
04  
05 // Integer extends Number
06  
07 List<? extends Number> foo3 = new ArrayList<? extends Integer>();
08  
09 // Double extends Number
10  
11 List<? extends Number> foo3 = new ArrayList<? extends Double>();
  1. Read operation by more than a given assignment, from what you have read to the type foo3 list of elements is it? You can read Number, either because of the above list contains elements Number, or contain elements Number of class.

    You can not guarantee to read Integer, because foo3 may point to a List <Double>.

    You can not guarantee to read Double, because foo3 may point to a List <Integer>.

  2. Write operations over more than a given assignment, you can what type of element inserted into the legitimate foo3 in it?

    You can not insert an Integer element because foo3 may point to List <Double>.

    You can not insert a Double element because foo3 may point to List <Integer>.

    You can not insert a Number element because foo3 may point to List <Integer>.

    You can not go List <? Extends T> insert any type of object, since you can not guarantee what kind of list actually points to is that you can not guarantee what type of object list is actually stored. The only guarantee is that you can read to a subclass of T or T.

super

Now consider List <? Super T>.

<? Super Integer> List foo3 wildcard declaration means the following assignment is legal:

01 // Integer is a "superclass" of Integer (in this context)
02  
03 List<? super Integer> foo3 = new ArrayList<Integer>();
04  
05 // Number is a superclass of Integer
06  
07 List<? super Integer> foo3 = new ArrayList<Number>();
08  
09 // Object is a superclass of Integer
10  
11 List<? super Integer> foo3 = new ArrayList<Object>();
  1. Read operation by more than a given assignment, from what you have read to the type foo3 list of elements is it? You can not guarantee to read Integer, because foo3 may point to List <Number> or List <Object>.

    You can not guarantee to read Number, because foo3 may point to List <Object>.

    The only guarantee is that you can read or Object Object Object subclass (you do not know what specific subclass).

  2. Write operations by more than a given assignment, you can what type of element inserted into the legitimate foo3 in it? You can insert an Integer object, because the list above statement of support Integer.

    You can insert an object subclass of Integer, Integer subclass because Integer is also, for the same reason.

    You can not insert Double object because foo3 may point ArrayList <Integer>.

    You can not insert Number object because foo3 may point ArrayList <Integer>.

    You can not insert Object object because foo3 may point ArrayList <Integer>.

PECS

Remember PECS principle: the producer (Producer) use extends, consumer (Consumer) using super.

  • Producers use extends

If you need to provide a list of elements of type T (ie T type you want to read an element from the list), you need to put this statement into the list of <? Extends T>, such as List <? Extends Integer>, so you can not go the list add any elements.

  • Consumers use super

If you need to use a list of elements of type T (ie T type of element you want to be added to the list), you need to put this statement into the list of <? Super T>, such as List <? Super Integer>, so you can not guarantee which the type of the read element.

  • That is the producers, but also consumers

If a list that is to be produced, but also the consumer, you can not use wildcards generic declaration list, such as List <Integer>.

example

Please refer to java.util.Collections in the copy method (JDK1.7):

 

Guess you like

Origin www.cnblogs.com/xiuyuanpingjie/p/10963036.html