????????
??????????Java???????????????????Java?????????з????????á??????????????????????Java??????????????漰???????????????????????????????
???????????
??????????
??????????????????????Box??
????public class Box {
????private String object;
????public void set(String object) { this.object = object; }
????public String get() { return object; }
????}
????????????????????????????????????Box??????????????String?????????????????????????Integer????????????????????????????д???Box???????ò??????????÷??????????????????
????public class Box<T> {
????// T stands for "Type"
????private T t;
????public void set(T t) { this.t = t; }
????public T get() { return t; }
????}
?????????????Box?????????????????????T?滻???κ???????????????
????Box<Integer> integerBox = new Box<Integer>();
????Box<Double> doubleBox = new Box<Double>();
????Box<String> stringBox = new Box<String>();
???????????
????????????????????????????????·???????????????????????????????????????????????????<K?? V>??????????
????public class Util {
????public static <K?? V> boolean compare(Pair<K?? V> p1?? Pair<K?? V> p2) {
????return p1.getKey().equals(p2.getKey()) &&
????p1.getValue().equals(p2.getValue());
????}
????}
????public class Pair<K?? V> {
????private K key;
????private V value;
????public Pair(K key?? V value) {
????this.key = key;
????this.value = value;
????}
????public void setKey(K key) { this.key = key; }
????public void setValue(V value) { this.value = value; }
????public K getKey()   { return key; }
????public V getValue() { return value; }
????}
?????????????????????????÷????????
????Pair<Integer?? String> p1 = new Pair<>(1?? "apple");
????Pair<Integer?? String> p2 = new Pair<>(2?? "pear");
????boolean same = Util.<Integer?? String>compare(p1?? p2);
??????????Java1.7/1.8????type inference????Java???????????????????????
????Pair<Integer?? String> p1 = new Pair<>(1?? "apple");
????Pair<Integer?? String> p2 = new Pair<>(2?? "pear");
????boolean same = Util.compare(p1?? p2);
????????
????????????????????????????????????????????д?????????????????????????????????
????public static <T> int countGreaterThan(T[] anArray?? T elem) {
????int count = 0;
????for (T e : anArray)
????if (e > elem)  // compiler error
????++count;
????return count;
????}
????????????????????????????????short?? int?? double?? long?? float?? byte?? char??????????????????????????ò?????>??????????????????????????????????????????????
????public interface Comparable<T> {
????public int compareTo(T o);
????}
??????????????????????????????????????????????????????T?????????????Comparable????????????????????????????????????compareTo??????
????public static <T extends Comparable<T>> int countGreaterThan(T[] anArray?? T elem) {
????int count = 0;
????for (T e : anArray)
????if (e.compareTo(elem) > 0)
????++count;
????return count;
????}
????????
??????????????????????????????????????????????????????漲???Box???????????????????????????
????public void boxTest(Box<Number> n) { /* ... */ }
???????????Box<Number> n?????????????????????????????????Box<Integer>????Box<Double>??????????????Integer??Double??Number?????????????????Box<Integer>????Box<Double>??Box<Number>???????κε???????????????????????????????????????????????????????
????????????????弸????????????????????????
????class Fruit {}
????class Apple extends Fruit {}
????class Orange extends Fruit {}
????????????????У???????????????????Reader???????f1()?е????????Fruit f = fruitReader.readExact(apples);??????????????List<Fruit>??List<Apple>???????κε?????
????public class GenericReading {
????static List<Apple> apples = Arrays.asList(new Apple());
????static List<Fruit> fruit = Arrays.asList(new Fruit());
????static class Reader<T> {
????T readExact(List<T> list) {
????return list.get(0);
????}
????}
????static void f1() {
????Reader<Fruit> fruitReader = new Reader<Fruit>();
????// Errors: List<Fruit> cannot be applied to List<Apple>.
????// Fruit f = fruitReader.readExact(apples);
????}
????public static void main(String[] args) {
????f1();
????}
????}
??????????????????????????Apple??Fruit?????????????????????????????????????????????н?????????????????????????????????????????
????static class CovariantReader<T> {
????T readCovariant(List<? extends T> list) {
????return list.get(0);
????}
????}
????static void f2() {
????CovariantReader<Fruit> fruitReader = new CovariantReader<Fruit>();
????Fruit f = fruitReader.readCovariant(fruit);
????Fruit a = fruitReader.readCovariant(apples);
????}
????public static void main(String[] args) {
????f2();
????}
?????????????????????? fruitReader??readCovariant????????????????????Fruit????????(????Fruit????)??????????????????????????????
????PECS???
?????????????????????<? extends T>???÷?????????????????list????get???????????????????list????add??????????????????£?
????public class GenericsAndCovariance {
????public static void main(String[] args) {
????// Wildcards allow covariance:
????List<? extends Fruit> flist = new ArrayList<Apple>();
????// Compile Error: can't add any type of object:
????// flist.add(new Apple())
????// flist.add(new Orange())
????// flist.add(new Fruit())
????// flist.add(new Object())
????flist.add(null); // Legal but uninteresting
????// We Know that it returns at least Fruit:
????Fruit f = flist.get(0);
????}
????}
???????????Java????????????????????????????????????????????????????????????????????List<? extends Fruit> flist??????????ж?????壺
????· List<? extends Fruit> flist = new ArrayList<Fruit>();
????· List<? extends Fruit> flist = new ArrayList<Apple>();
????· List<? extends Fruit> flist = new ArrayList<Orange>();
?????????????add???Apple?????flist???????new ArrayList<Orange>();
?????????????add???Orange?????flist???????new ArrayList<Apple>();
?????????????add???Fruit????????Fruit???????κ??????Fruit????flist??????????????????Fruit??????????????????????
????????????????<? extends T>????????????????Producer??????(get)?????????????Consumer????????(add)????
????????????add?????????????????????<? super T>??
????public class GenericWriting {
????static List<Apple> apples = new ArrayList<Apple>();
????static List<Fruit> fruit = new ArrayList<Fruit>();
????static <T> void writeExact(List<T> list?? T item) {
????list.add(item);
????}
????static void f1() {
????writeExact(apples?? new Apple());
????writeExact(fruit?? new Apple());
????}
????static <T> void writeWithWildcard(List<? super T> list?? T item) {
????list.add(item)
????}
????static void f2() {
????writeWithWildcard(apples?? new Apple());
????writeWithWildcard(fruit?? new Apple());
????}
????public static void main(String[] args) {
????f1(); f2();
????}
????}
?????????????????????????????????????????super????????????get??????????????????????????????????????????????????????List<? super Apple> list?????????????漸????壺
????· List<? super Apple> list = new ArrayList<Apple>();
????· List<? super Apple> list = new ArrayList<Fruit>();
????· List<? super Apple> list = new ArrayList<Object>();
????????????????list??get???Apple??????????get??????Fruit?????Fruit??????Orange???????????Fruit??
?????????????????????????????????????”Producer Extends?? Consumer Super”??
????· “Producer Extends” – ??????????????List????????produce T????????? extends T??
????· “Consumer Super” – ????????????дList????????consume T????????? super T??
????· ??????????????д??????????????????????
?????????????ЩJava?????????????????????????????????????????????????????????
????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));
????}
????}