Java官方筆記8泛型

泛型

爲什麼需要泛型?generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods.

說白了就像Python動態語言的變量,是動態的,可以指向任意類型。

泛型有個好處是不需要類型轉換:

List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);
List<String> list = new ArrayList<String>();
list.add("hello");
String s = list.get(0);   // no cast

這個例子的泛型,是指List的實現使用了泛型<T>,從而給使用帶來了好處。

定義

class name<T1, T2, ..., Tn> { /* ... */ }

比如定義class List<T>,使用List<String>

將以下代碼:

public class Box {
    private Object object;

    public void set(Object object) { this.object = object; }
    public Object get() { return object; }
}

改爲泛型實現:

/**
 * Generic version of the Box class.
 * @param <T> the type of the value being boxed
 */
public class Box<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
}

T必須是非基本數據類型:any class type, any interface type, any array type, or even another type variable

type parameter命名採用單個大寫字母:

  • E - Element (used extensively by the Java Collections Framework)
  • K - Key
  • N - Number
  • T - Type
  • V - Value
  • S, U, V etc. - 2nd, 3rd, 4th types

這樣能很好的跟其他命名區分開來。

使用泛型,必須要指定具體的值,比如這裏的Integer:

Box<Integer> integerBox;

這就跟方法調用傳參是一個道理。

Diamond,將:

Box<Integer> integerBox = new Box<Integer>();

簡寫爲:

Box<Integer> integerBox = new Box<>();

多個type parameters

public interface Pair<K, V> {
    public K getKey();
    public V getValue();
}

public class OrderedPair<K, V> implements Pair<K, V> {

    private K key;
    private V value;

    public OrderedPair(K key, V value) {
    this.key = key;
    this.value = value;
    }

    public K getKey()    { return key; }
    public V getValue() { return value; }
}
Pair<String, Integer> p1 = new OrderedPair<String, Integer>("Even", 8);  // 這裏的int類型8,自動裝箱爲了I
Pair<String, String>  p2 = new OrderedPair<String, String>("hello", "world");

簡寫爲diamond:

OrderedPair<String, Integer> p1 = new OrderedPair<>("Even", 8);
OrderedPair<String, String>  p2 = new OrderedPair<>("hello", "world");

嵌套:

OrderedPair<String, Box<Integer>> p = new OrderedPair<>("primes", new Box<Integer>(...));

Raw Types

raw type is the name of a generic class or interface without any type arguments.

比如:

public class Box<T> {
    public void set(T t) { /* ... */ }
    // ...
}
Box<Integer> intBox = new Box<>();
Box rawBox = new Box();  // 這個就是rawType

在IDEA有時候會碰到警告Raw use of parameterized class 'List',就是指的這個玩意。這是老式寫法,raw types會繞過泛型的type checks,應該避免使用。

Generic Methods

定義,泛型位置在return type的前面:

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);

調用泛型方法時也可以省略泛型入參:

Pair<Integer, String> p1 = new Pair<>(1, "apple");
Pair<Integer, String> p2 = new Pair<>(2, "pear");
boolean same = Util.compare(p1, p2);  // 這裏省略了泛型入參

Bounded Type Parameters

有點像Python的typing,限制動態變量的類型,使用extends關鍵字:

public class Box<T> {

    private T t;          

    public void set(T t) {
        this.t = t;
    }

    public T get() {
        return t;
    }

    public <U extends Number> void inspect(U u){
        System.out.println("T: " + t.getClass().getName());
        System.out.println("U: " + u.getClass().getName());
    }

    public static void main(String[] args) {
        Box<Integer> integerBox = new Box<Integer>();
        integerBox.set(new Integer(10));
        integerBox.inspect("some text"); // error: this is still String!
    }
}

這樣還能進一步調用bounded type parameters的方法:

public class NaturalNumber<T extends Integer> {

    private T n;

    public NaturalNumber(T n)  { this.n = n; }

    public boolean isEven() {
        return n.intValue() % 2 == 0;  // intValue()是Integer的方法
    }

    // ...
}

Multiple Bounds

Class A { /* ... */ }
interface B { /* ... */ }
interface C { /* ... */ }

class D <T extends A & B & C> { /* ... */ }

class的位置必須在interface前面。

Bounded Type Parameters的用途之一,比如:

public static <T> int countGreaterThan(T[] anArray, T elem) {
    int count = 0;
    for (T e : anArray)
        if (e > elem)  // compiler error
            ++count;
    return count;
}

會編譯報錯,因爲>符號只適用基本數據類型,如果想支持Object,怎麼辦呢:

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;
}

extends Comparable<T>以後調用compareTo()方法,就能既支持基本數據類型又能支持Object了。

泛型在繼承時有個注意的點: Box<Integer> and Box<Double> are not subtypes of Box<Number>

正確的方式:

You can subtype a generic class or interface by extending or implementing it.

interface PayloadList<E,P> extends List<E> {
  void setPayload(int index, P val);
  ...
}

Type Inference

類型推斷:Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable.(換個理解方式,就是動態變量需要知道綁定哪個類型)

Diamond就是一種Type Inference:

Map<String, List<String>> myMap = new HashMap<>();

在構造方法中進行推斷:

class MyClass<X> {
  <T> MyClass(T t) {
    // ...
  }
}
MyClass<Integer> myObject = new MyClass<>("");

X推斷爲Integer,T推斷爲String。

Lambda Expressions也會根據上下文推斷target type:

public static void printPersons(List<Person> roster, CheckPerson tester)
public void printPersonsWithPredicate(List<Person> roster, Predicate<Person> tester) 
printPersons(
        people, 
        p -> p.getGender() == Person.Sex.MALE
            && p.getAge() >= 18
            && p.getAge() <= 25);  // 自動推斷爲CheckPerson
printPersonsWithPredicate(
        people,
        p -> p.getGender() == Person.Sex.MALE
             && p.getAge() >= 18
             && p.getAge() <= 25);)  // 自動推斷Predicate<Person>
  • Variable declarations
  • Assignments
  • Return statements
  • Array initializers
  • Method or constructor arguments
  • Lambda expression bodies
  • Conditional expressions, ?:
  • Cast expressions

再看個例子:

public interface Runnable {
    void run();
}

public interface Callable<V> {
    V call();
}
void invoke(Runnable r) {
    r.run();
}

<T> T invoke(Callable<T> c) {
    return c.call();
}
String s = invoke(() -> "done");  // Lambda 

實際推斷使用哪個?答案是Callable,因爲它有return,而Runnable沒有。

Wildcards

使用?extends表示上限:

List<? extends Number>

可以是 List<Integer>List<Double>, and List<Number>

這裏的extends既是class的extends,也是interface的implements。

List<Object>List<?>有什麼區別?

①子類型

public static void printList(List<Object> list) {
    for (Object elem : list)
        System.out.println(elem + " ");
    System.out.println();
}

it prints only a list of Object instances; it cannot print List<Integer>List<String>List<Double>, and so on, because they are not subtypes of List<Object>.

public static void printList(List<?> list) {
    for (Object elem: list)
        System.out.print(elem + " ");
    System.out.println();
}

Because for any concrete type AList<A> is a subtype of List<?>, you can use printList() to print a list of any type.

②值

You can insert an Object, or any subtype of Object, into a List<Object>. But you can only insert null into a List<?>.

使用?super表示下限:

public static void addNumbers(List<? super Integer> list) {
    for (int i = 1; i <= 10; i++) {
        list.add(i);
    }
}

可以是List<Integer>List<Number>, and List<Object> — anything that can hold Integer values

?能支持集合子類型:

Type Erasure

Type Erasure是Java編譯器爲了實現泛型做的:

  • Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
  • Insert type casts if necessary to preserve type safety.
  • Generate bridge methods to preserve polymorphism in extended generic types.

Restriction on Generics

1、不能使用基本數據類型:

class Pair<K, V> {

    private K key;
    private V value;

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    // ...
}
Pair<int, char> p = new Pair<>(8, 'a');  // compile-time error

只能使用包裝類:

Pair<Integer, Character> p = new Pair<>(8, 'a');

2、不能創建泛型實例:

public static <E> void append(List<E> list) {
    E elem = new E();  // compile-time error
    list.add(elem);
}

只能通過類來創建實例:

public static <E> void append(List<E> list, Class<E> cls) throws Exception {
    E elem = cls.newInstance();   // OK
    list.add(elem);
}
List<String> ls = new ArrayList<>();
append(ls, String.class);

3、static不能使用泛型

public class MobileDevice<T> {
    private static T os;  // 在實例化後,會同時代表3種類型,顯然不合理

    // ...
}
MobileDevice<Smartphone> phone = new MobileDevice<>();
MobileDevice<Pager> pager = new MobileDevice<>();
MobileDevice<TabletPC> pc = new MobileDevice<>();

4、不能instanceof

public static <E> void rtti(List<E> list) {
    if (list instanceof ArrayList<Integer>) {  // compile-time error
        // ...
    }
}

使用?可以:

public static void rtti(List<?> list) {
    if (list instanceof ArrayList<?>) {  // OK; instanceof requires a reifiable type
        // ...
    }
}

5、不能創建泛型數組:

List<Integer>[] arrayOfLists = new List<Integer>[2];  // compile-time error

6、Cannot Create, Catch, or Throw Objects of Parameterized Types

// Extends Throwable indirectly
class MathException<T> extends Exception { /* ... */ }    // compile-time error

// Extends Throwable directly
class QueueFullException<T> extends Throwable { /* ... */ // compile-time error
public static <T extends Exception, J> void execute(List<J> jobs) {
    try {
        for (J job : jobs)
            // ...
    } catch (T e) {   // compile-time error
        // ...
    }
}

throws可以:

class Parser<T extends Exception> {
    public void parse(File file) throws T {     // OK
        // ...
    }
}

7、重載方法不能有擦除後相同的泛型:

public class Example {
    public void print(Set<String> strSet) { }
    public void print(Set<Integer> intSet) { }  // 編譯錯誤
}

參考資料:

Generics https://dev.java/learn/generics/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章