通常的類和方法,只能使用具體的類型:要麼是基本類型,要麼是自定義類型。若是要編寫能夠應用於多種類型的代碼,這種刻板的限制對代碼的束縛就會很大。
在面對對象編程語言中,多態算是一種泛化機制。
泛型實現了參數化類型的概念,使代碼能夠應用於多種類型。java
創造容器類促進了泛型的出現。
下面是隻能持有單個對象的類:apache
class Automobile {} public class Holder1 { private Automobile a; public Holder1(Automobile a) { this.a = a; } Automobile get() { return a; } }
直接持有Object類型的對象,它能夠存儲任何類型的對象:編程
public class Holder2 { private Object a; public Holder2(Object a) { this.a = a; } public void set(Object a) { this.a = a; } public Object get() { return a; } public static void main(String[] args) { Holder2 h2 = new Holder2(new Automobile()); Automobile a = (Automobile)h2.get(); h2.set("Not an Automobile"); String s = (String)h2.get(); h2.set(1); // Autoboxes to Integer Integer x = (Integer)h2.get(); } }
使用泛型定義:設計模式
public class Holder3<T> { private T a; public Holder3(T a) { this.a = a; } public void set(T a) { this.a = a; } public T get() { return a; } public static void main(String[] args) { Holder3<Automobile> h3 = new Holder3<Automobile>(new Automobile()); Automobile a = h3.get(); // No cast needed // h3.set("Not an Automobile"); // Error // h3.set(1); // Error } }
元組它是將一組對象直接打包存儲於其中一個單一對象。這個容器對象容許讀取其中元素,可是不容許向其中存放新的對象。
元組能夠具備任意長度,元組中的對象能夠是任意不一樣的類型。數組
//: generics/TupleTest.java import net.mindview.util.*; class Amphibian {} class Vehicle {} public class TupleTest { static TwoTuple<String,Integer> f() { // Autoboxing converts the int to Integer: return new TwoTuple<String,Integer>("hi", 47); } static ThreeTuple<Amphibian,String,Integer> g() { return new ThreeTuple<Amphibian, String, Integer>( new Amphibian(), "hi", 47); } static FourTuple<Vehicle,Amphibian,String,Integer> h() { return new FourTuple<Vehicle,Amphibian,String,Integer>( new Vehicle(), new Amphibian(), "hi", 47); } static FiveTuple<Vehicle,Amphibian,String,Integer,Double> k() { return new FiveTuple<Vehicle,Amphibian,String,Integer,Double>( new Vehicle(), new Amphibian(), "hi", 47, 11.1); } public static void main(String[] args) { TwoTuple<String,Integer> ttsi = f(); System.out.println(ttsi); // ttsi.first = "there"; // Compile error: final System.out.println(g()); System.out.println(h()); System.out.println(k()); } } /* Output: (80% match) (hi, 47) (Amphibian@1f6a7b9, hi, 47) (Vehicle@35ce36, Amphibian@757aef, hi, 47) (Vehicle@9cab16, Amphibian@1a46e30, hi, 47, 11.1) *///:~
public class LinkedStack<T> { private static class Node<U> { U item; Node<U> next; Node() { item = null; next = null; } Node(U item, Node<U> next) { this.item = item; this.next = next; } boolean end() { return item == null && next == null; } } private Node<T> top = new Node<T>(); // End sentinel public void push(T item) { top = new Node<T>(item, top); } public T pop() { T result = top.item; if(!top.end()) top = top.next; return result; } public static void main(String[] args) { LinkedStack<String> lss = new LinkedStack<String>(); for(String s : "Phasers on stun!".split(" ")) lss.push(s); String s; while((s = lss.pop()) != null) System.out.println(s); } } /* Output: stun! on Phasers *///:~
//: generics/RandomList.java import java.util.*; public class RandomList<T> { private ArrayList<T> storage = new ArrayList<T>(); private Random rand = new Random(47); public void add(T item) { storage.add(item); } public T select() { return storage.get(rand.nextInt(storage.size())); } public static void main(String[] args) { RandomList<String> rs = new RandomList<String>(); for(String s: ("The quick brown fox jumped over " + "the lazy brown dog").split(" ")) rs.add(s); for(int i = 0; i < 11; i++) System.out.print(rs.select() + " "); } } /* Output: brown over fox quick quick dog brown The brown lazy brown *///:~
泛型能夠應用於接口。例如生成器,這是一種專門負責建立對象的類。這是工廠方法設計模式的一種應用。不過,當使用生成器建立新對象時,它不須要任何參數,而工廠方法通常須要參數。安全
package coffee; public class Americano extends Coffee {} . . .
實現Generator< Coffee >接口,隨機生成不一樣類型對象:app
package coffee; import java.util.*; import net.mindview.util.*; public class CoffeeGenerator implements Generator<Coffee>, Iterable<Coffee> { private Class[] types = { Latte.class, Mocha.class, Cappuccino.class, Americano.class, Breve.class, }; private static Random rand = new Random(47); public CoffeeGenerator() {} // For iteration: private int size = 0; public CoffeeGenerator(int sz) { size = sz; } public Coffee next() { try { return (Coffee) types[rand.nextInt(types.length)].newInstance(); // Report programmer errors at run time: } catch(Exception e) { throw new RuntimeException(e); } } class CoffeeIterator implements Iterator<Coffee> { int count = size; public boolean hasNext() { return count > 0; } public Coffee next() { count--; return CoffeeGenerator.this.next(); } public void remove() { // Not implemented throw new UnsupportedOperationException(); } } public Iterator<Coffee> iterator() { return new CoffeeIterator(); } public static void main(String[] args) { CoffeeGenerator gen = new CoffeeGenerator(); for(int i = 0; i < 5; i++) System.out.println(gen.next()); for(Coffee c : new CoffeeGenerator(5)) System.out.println(c); } }
使用它生成Fibonacci數列:dom
import net.mindview.util.*; public class Fibonacci implements Generator<Integer> { private int count = 0; public Integer next() { return fib(count++); } private int fib(int n) { if(n < 2) return 1; return fib(n-2) + fib(n-1); } public static void main(String[] args) { Fibonacci gen = new Fibonacci(); for(int i = 0; i < 18; i++) System.out.print(gen.next() + " "); } }
泛型的一個侷限性:基本類型沒法做爲類型參數,Java SE5具有了自動打包和自動拆包的功能。
經過繼承建立適配器:編程語言
import java.util.*; public class IterableFibonacci extends Fibonacci implements Iterable<Integer> { private int n; public IterableFibonacci(int count) { n = count; } public Iterator<Integer> iterator() { return new Iterator<Integer>() { public boolean hasNext() { return n > 0; } public Integer next() { n--; return IterableFibonacci.this.next(); } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } public static void main(String[] args) { for(int i : new IterableFibonacci(18)) System.out.print(i + " "); } }
基本的指導原則:不管什麼時候,只要你能作到,你就應該儘可能使用泛型方法。對於一個static方法而言,沒法訪問泛型類的類型參數,若是static方法須要使用泛型能力,就必須時期成爲泛型方法。
定義泛型方法,只需將泛型參數列表置於返回值以前:函數
public class GenericMethods { public <T> void f(T x) { System.out.println(x.getClass().getName()); } public static void main(String[] args) { GenericMethods gm = new GenericMethods(); gm.f(""); gm.f(1); gm.f(1.0); gm.f(1.0F); gm.f('c'); gm.f(gm); } } /* Output: java.lang.String java.lang.Integer java.lang.Double java.lang.Float java.lang.Character GenericMethods *///:~
使用泛型方法時,一般沒必要指定參數類型,編譯器會找出具體的類型,這稱爲類型參數推斷。
若是給泛型方法傳入基本類型,自動打包機制就會介入其中,將基本類型的值包裝爲對應的對象。
編寫一個工具類,它包含各類各樣的static方法,專門建立各類容器對象:
public class New { public static <K,V> Map<K,V> map(){ return new HashMap<K,V>(); } }
import pets.*; import java.util.*; import net.mindview.util.*; public class SimplerPets { public static void main(String[] args) { Map<Person, List<? extends Pet>> petPeople = New.map(); // Rest of the code is the same... } } ///:~
類型推斷只對賦值操做有效,其餘時候並不起做用,若是將一個泛型方法調用的結果做爲參數,傳遞給另外一個方法,這是編譯器是不會執行類型推斷。編譯器認爲:調用泛型方法後,器返回值被賦給一個Object類型的變量。
要顯式的指明類型,必須再點操做符與方法名之間插入尖括號,而後把類型置於尖括號內:
import pets.*; import java.util.*; import net.mindview.util.*; public class ExplicitTypeSpecification { static void f(Map<Person, List<Pet>> petPeople) {} public static void main(String[] args) { f(New.<Person, List<Pet>>map()); } } ///:~
泛型方法與可變參數列表可以很好的共存:
import java.util.*; public class GenericVarargs { public static <T> List<T> makeList(T... args) { List<T> result = new ArrayList<T>(); for(T item : args) result.add(item); return result; } public static void main(String[] args) { List<String> ls = makeList("A"); System.out.println(ls); ls = makeList("A", "B", "C"); System.out.println(ls); ls = makeList("ABCDEFFHIJKLMNOPQRSTUVWXYZ".split("")); System.out.println(ls); } } /* Output: [A] [A, B, C] [, A, B, C, D, E, F, F, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z] *///:~
import coffee.*; import java.util.*; import net.mindview.util.*; public class Generators { public static <T> Collection<T> fill(Collection<T> coll, Generator<T> gen, int n) { for(int i = 0; i < n; i++) coll.add(gen.next()); return coll; } public static void main(String[] args) { Collection<Coffee> coffee = fill( new ArrayList<Coffee>(), new CoffeeGenerator(), 4); for(Coffee c : coffee) System.out.println(c); Collection<Integer> fnumbers = fill( new ArrayList<Integer>(), new Fibonacci(), 12); for(int i : fnumbers) System.out.print(i + ", "); } } /* Output: Americano 0 Latte 1 Americano 2 Mocha 3 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, *///:~
public class CountedObject { private static long counter = 0; private final long id = counter++; public long id() { return id; } public String toString() { return "CountedObject " + id;} } ///:~
import net.mindview.util.*; public class BasicGeneratorDemo { public static void main(String[] args) { Generator<CountedObject> gen = BasicGenerator.create(CountedObject.class); for(int i = 0; i < 5; i++) System.out.println(gen.next()); } } /* Output: CountedObject 0 CountedObject 1 CountedObject 2 CountedObject 3 CountedObject 4 *///:~
//: generics/TupleTest2.java import net.mindview.util.*; import static net.mindview.util.Tuple.*; public class TupleTest2 { static TwoTuple<String,Integer> f() { return tuple("hi", 47); } static TwoTuple f2() { return tuple("hi", 47); } static ThreeTuple<Amphibian,String,Integer> g() { return tuple(new Amphibian(), "hi", 47); } static FourTuple<Vehicle,Amphibian,String,Integer> h() { return tuple(new Vehicle(), new Amphibian(), "hi", 47); } static FiveTuple<Vehicle,Amphibian,String,Integer,Double> k() { return tuple(new Vehicle(), new Amphibian(), "hi", 47, 11.1); } public static void main(String[] args) { TwoTuple<String,Integer> ttsi = f(); System.out.println(ttsi); System.out.println(f2()); System.out.println(g()); System.out.println(h()); System.out.println(k()); } } /* Output: (80% match) (hi, 47) (hi, 47) (Amphibian@7d772e, hi, 47) (Vehicle@757aef, Amphibian@d9f9c3, hi, 47) (Vehicle@1a46e30, Amphibian@3e25a5, hi, 47, 11.1) *///:~
泛型還能夠應用於內部類以及匿名內部類,使用匿名內部類實現Generator接口:
//: generics/BankTeller.java // A very simple bank teller simulation. import java.util.*; import net.mindview.util.*; class Customer { private static long counter = 1; private final long id = counter++; private Customer() {} public String toString() { return "Customer " + id; } // A method to produce Generator objects: public static Generator<Customer> generator() { return new Generator<Customer>() { public Customer next() { return new Customer(); } }; } } class Teller { private static long counter = 1; private final long id = counter++; private Teller() {} public String toString() { return "Teller " + id; } // A single Generator object: public static Generator<Teller> generator = new Generator<Teller>() { public Teller next() { return new Teller(); } }; } public class BankTeller { public static void serve(Teller t, Customer c) { System.out.println(t + " serves " + c); } public static void main(String[] args) { Random rand = new Random(47); Queue<Customer> line = new LinkedList<Customer>(); Generators.fill(line, Customer.generator(), 15); List<Teller> tellers = new ArrayList<Teller>(); Generators.fill(tellers, Teller.generator, 4); for(Customer c : line) serve(tellers.get(rand.nextInt(tellers.size())), c); } } /* Output: Teller 3 serves Customer 1 Teller 2 serves Customer 2 Teller 3 serves Customer 3 Teller 1 serves Customer 4 Teller 1 serves Customer 5 Teller 3 serves Customer 6 Teller 1 serves Customer 7 Teller 2 serves Customer 8 Teller 3 serves Customer 9 Teller 3 serves Customer 10 Teller 2 serves Customer 11 Teller 4 serves Customer 12 Teller 2 serves Customer 13 Teller 1 serves Customer 14 Teller 1 serves Customer 15 *///:~
泛型的一個重要好處是可以簡單而安全的建立複雜的模型。
建立List元組:
//: generics/TupleList.java // Combining generic types to make complex generic types. import java.util.*; import net.mindview.util.*; public class TupleList<A,B,C,D> extends ArrayList<FourTuple<A,B,C,D>> { public static void main(String[] args) { TupleList<Vehicle, Amphibian, String, Integer> tl = new TupleList<Vehicle, Amphibian, String, Integer>(); tl.add(TupleTest.h()); tl.add(TupleTest.h()); for(FourTuple<Vehicle,Amphibian,String,Integer> i: tl) System.out.println(i); } } /* Output: (75% match) (Vehicle@11b86e7, Amphibian@35ce36, hi, 47) (Vehicle@757aef, Amphibian@d9f9c3, hi, 47) *///:~
構建一個商店,它包括走廊,貨架和商品:
//: generics/Store.java // Building up a complex model using generic containers. import java.util.*; import net.mindview.util.*; //商品 class Product { private final int id; private String description; private double price; public Product(int IDnumber, String descr, double price){ id = IDnumber; description = descr; this.price = price; System.out.println(toString()); } public String toString() { return id + ": " + description + ", price: $" + price; } public void priceChange(double change) { price += change; } public static Generator<Product> generator = new Generator<Product>() { private Random rand = new Random(47); public Product next() { return new Product(rand.nextInt(1000), "Test", Math.round(rand.nextDouble() * 1000.0) + 0.99); } }; } //貨架 class Shelf extends ArrayList<Product> { public Shelf(int nProducts) { Generators.fill(this, Product.generator, nProducts); } } //走廊 class Aisle extends ArrayList<Shelf> { public Aisle(int nShelves, int nProducts) { for(int i = 0; i < nShelves; i++) add(new Shelf(nProducts)); } } class CheckoutStand {} class Office {} //商店 public class Store extends ArrayList<Aisle> { private ArrayList<CheckoutStand> checkouts = new ArrayList<CheckoutStand>(); private Office office = new Office(); public Store(int nAisles, int nShelves, int nProducts) { for(int i = 0; i < nAisles; i++) add(new Aisle(nShelves, nProducts)); } public String toString() { StringBuilder result = new StringBuilder(); for(Aisle a : this) for(Shelf s : a) for(Product p : s) { result.append(p); result.append("\n"); } return result.toString(); } public static void main(String[] args) { System.out.println(new Store(14, 5, 10)); } } /* Output: 258: Test, price: $400.99 861: Test, price: $160.99 868: Test, price: $417.99 207: Test, price: $268.99 551: Test, price: $114.99 278: Test, price: $804.99 520: Test, price: $554.99 140: Test, price: $530.99 ... *///:~
咱們能夠聲明ArrayList.class,但不能聲明ArrayList< Integer >.class
import java.util.*; public class ErasedTypeEquivalence { public static void main(String[] args) { Class c1 = new ArrayList<String>().getClass(); Class c2 = new ArrayList<Integer>().getClass(); System.out.println(c1 == c2); } } /* Output: true *///:~
//: generics/LostInformation.java import java.util.*; class Frob {} class Fnorkle {} class Quark<Q> {} class Particle<POSITION,MOMENTUM> {} public class LostInformation { public static void main(String[] args) { List<Frob> list = new ArrayList<Frob>(); Map<Frob,Fnorkle> map = new HashMap<Frob,Fnorkle>(); Quark<Fnorkle> quark = new Quark<Fnorkle>(); Particle<Long,Double> p = new Particle<Long,Double>(); System.out.println(Arrays.toString( list.getClass().getTypeParameters())); System.out.println(Arrays.toString( map.getClass().getTypeParameters())); System.out.println(Arrays.toString( quark.getClass().getTypeParameters())); System.out.println(Arrays.toString( p.getClass().getTypeParameters())); } } /* Output: [E] [K, V] [Q] [POSITION, MOMENTUM] *///:~
在泛型代碼內部,沒法得到任何有關泛型參數類型的信息。
Java泛型是使用擦除來實現的,這意味着當你在使用泛型時,任何具體的類型信息都被擦除了,你爲一知道的就是你在使用一個對象。
public class HasF { public void f() { System.out.println("HasF.f()"); } } ///:~
//: generics/Manipulation.java // {CompileTimeError} (Won't compile) class Manipulator<T> { private T obj; public Manipulator(T x) { obj = x; } // Error: cannot find symbol: method f(): public void manipulate() {/* obj.f();沒法找到類型*/ } } public class Manipulation { public static void main(String[] args) { HasF hf = new HasF(); Manipulator<HasF> manipulator = new Manipulator<HasF>(hf); manipulator.manipulate(); } } ///:~
]因爲有了擦除,Java編譯器沒法將manipulate()必須可以在obj上調用f()着一需求映射到HasF擁有f()這一事實。
爲了調用f(),咱們重用extends關鍵字:
class Manipulator2<T extends HasF> { private T obj; public Manipulator2(T x) { obj = x; } public void manipulate() { obj.f(); } } ///:~
咱們說泛型類型參數將擦除到它的第一個邊界,咱們還提到了類型參數的擦除。編譯器實際上會把類型參數替換爲它的擦除,就像上面的同樣。T擦除到了HasF,就好像在類的聲明中用HasF替換了T同樣。
只有當你但願使用的類型參數比某個具體類型更加泛化時——也就是說,當你但願代碼可以跨多個類工做時。
class ReturnGenericType<T extends HasF> { private T obj; public ReturnGenericType(T x) { obj = x; } public T get() { return obj; } } ///:~
爲了減小潛在的關於擦除的混淆,你必須清楚的認識到這不是一個語言特性。它是Java的泛型實現中的一種折中,由於泛型不是Java語言出現時就有的部分。
在基於擦除的實現中,泛型類型被看成第二類型處理,即不能再某些重要的上下文環境中使用的類型。泛型類型只有再靜態類型檢查期間纔出現,再此以後,程序中全部泛型類型都將被擦除,替換爲它們的非泛型上界。例如:諸如List< T >這樣的類型註解將被擦除爲List而普通的類型變量再未指定邊界的狀況下被擦除未Object。
擦除的核心動機是它使得泛化客戶端能夠用非泛化得類庫來使用。這被稱爲遷移兼容性。
擦除主要的正當理由是從非泛型代碼到泛型代碼的轉變過程,以及再不破壞現有類型的狀況下,將泛型融入Java語言。
建立實例:
class Foo<T>{ T var; } Foo<Cat> f=new Foo<Cat>();
並非再整個類中的各個地方,類型T都在被替換,編寫這個類的代碼時,必須知道,它只是一個Object。
//: generics/ErasureAndInheritance.java class GenericBase<T> { private T element; public void set(T arg) { arg = element; } public T get() { return element; } } class Derived1<T> extends GenericBase<T> {} class Derived2 extends GenericBase {} // No warning // class Derived3 extends GenericBase<?> {} // Strange error: // unexpected type found : ? // required: class or interface without bounds public class ErasureAndInheritance { @SuppressWarnings("unchecked") public static void main(String[] args) { Derived2 d2 = new Derived2(); Object obj = d2.get(); d2.set(obj); // Warning here! } } ///:~
@SuppressWarnings("unchecked")
這個註解被放置再能夠產生這類警告的方法智商,而不是整個類上。當你要關閉警告時,最好時儘可能聚焦,這樣就不會由於過於寬泛的關閉警告,而致使意外遮蔽掉真正的問題。
正是由於有了擦除,泛型最讓人困惑的地方就是能夠表示沒有任何意義的事物:
//: generics/ArrayMaker.java import java.lang.reflect.*; import java.util.*; public class ArrayMaker<T> { private Class<T> kind; public ArrayMaker(Class<T> kind) { this.kind = kind; } @SuppressWarnings("unchecked") T[] create(int size) { return (T[])Array.newInstance(kind, size); } public static void main(String[] args) { ArrayMaker<String> stringMaker = new ArrayMaker<String>(String.class); String[] stringArray = stringMaker.create(9); System.out.println(Arrays.toString(stringArray)); } } /* Output: [null, null, null, null, null, null, null, null, null] *///:~
對於建立數組,使用Array.newInstance()是推薦方式。
import java.util.*; public class ListMaker<T> { List<T> create() { //這其中的<T>被移除了,在運行時,這個內部沒有任何<T> return new ArrayList<T>(); } public static void main(String[] args) { ListMaker<String> stringMaker= new ListMaker<String>(); List<String> stringList = stringMaker.create(); } } ///:~
在返回list以前,將某些對象放入其中:
import java.util.*; public class FilledListMaker<T> { List<T> create(T t, int n) { List<T> result = new ArrayList<T>(); for(int i = 0; i < n; i++) result.add(t); return result; } public static void main(String[] args) { FilledListMaker<String> stringMaker = new FilledListMaker<String>(); List<String> list = stringMaker.create("Hello", 4); System.out.println(list); } } /* Output: [Hello, Hello, Hello, Hello] *///:~
這裏即便編譯器沒法知道有關create()中的T的任何信息,可是它仍舊能夠在編譯期確保放置到result中的對象具備T類型,使其適合ArrayList< T >。即便擦除在方法或內部移除了有關實際類型的信息,編譯器仍舊能夠確保在方法或類中使用的類型的內部一致性。
擦除在方法體中移除了類型信息,全部在運行時的問題就邊界:即對象進入和離開方法的地點。
泛型中的全部動做都發生在邊界處——對傳遞進來的值進行額外的編譯期檢查,並插入對傳遞出去的值的轉型。邊界就是發生動做的地方
擦除丟失了在泛型代碼中執行某些操做的能力。任何運行時須要知道確切類型信息的操做都將沒法工做:
public class Erased<T> { private final int SIZE = 100; public static void f(Object arg) {//在這裏類型信息被擦除了。 if(arg instanceof T) {} // Error T var = new T(); // Error new T沒法實現,部分緣由時擦出,另外一部分是由於編譯器不能驗證T具備默認構造器 T[] array = new T[SIZE]; // Error T[] array = (T)new Object[100]; // Unchecked warning } } ///:~
若是引入類型標籤,就能夠轉而使用動態的isInstance():
class Building {} class House extends Building {} public class ClassTypeCapture<T> { Class<T> kind; public ClassTypeCapture(Class<T> kind) { this.kind = kind; } public boolean f(Object arg) { return kind.isInstance(arg); } public static void main(String[] args) { ClassTypeCapture<Building> ctt1 = new ClassTypeCapture<Building>(Building.class); System.out.println(ctt1.f(new Building())); System.out.println(ctt1.f(new House())); ClassTypeCapture<House> ctt2 = new ClassTypeCapture<House>(House.class); System.out.println(ctt2.f(new Building())); System.out.println(ctt2.f(new House())); } } /* Output: true true false true *///:~
傳遞一個工廠對象,並使用它來建立新的實例:
import static net.mindview.util.Print.*; class ClassAsFactory<T> { T x; public ClassAsFactory(Class<T> kind) { try { x = kind.newInstance(); } catch(Exception e) { throw new RuntimeException(e); } } } class Employee {} public class InstantiateGenericType { public static void main(String[] args) { ClassAsFactory<Employee> fe = new ClassAsFactory<Employee>(Employee.class); print("ClassAsFactory<Employee> succeeded"); try { ClassAsFactory<Integer> fi = new ClassAsFactory<Integer>(Integer.class);//將會失敗,由於Integer沒有默認的構造參數 } catch(Exception e) { print("ClassAsFactory<Integer> failed"); } } } /* Output: ClassAsFactory<Employee> succeeded ClassAsFactory<Integer> failed *///:~
使用顯示的工廠,並限制類型:
interface FactoryI<T> { T create(); } class Foo2<T> { private T x; public <F extends FactoryI<T>> Foo2(F factory) { x = factory.create(); } // ... } class IntegerFactory implements FactoryI<Integer> { public Integer create() { return new Integer(0); } } class Widget { public static class Factory implements FactoryI<Widget> { public Widget create() { return new Widget(); } } } public class FactoryConstraint { public static void main(String[] args) { new Foo2<Integer>(new IntegerFactory()); new Foo2<Widget>(new Widget.Factory()); } } ///:~
模板方法設計模式:
abstract class GenericWithCreate<T> { final T element; GenericWithCreate() { element = create(); } abstract T create(); } class X {} class Creator extends GenericWithCreate<X> { X create() { return new X(); } void f() { System.out.println(element.getClass().getSimpleName()); } } public class CreatorGeneric { public static void main(String[] args) { Creator c = new Creator(); c.f(); } } /* Output: X *///:~
不能建立泛型數組。通常的解決辦法是再任何想要建立泛型數組的地方都使用ArrayList:
import java.util.*; public class ListOfGenerics<T> { private List<T> array = new ArrayList<T>(); public void add(T item) { array.add(item); } public T get(int index) { return array.get(index); } } ///:~
能夠按照編譯器喜歡方法定義一個引用:
class Generic<T> {} public class ArrayOfGenericReference { static Generic<Integer>[] gia; } ///:~
編譯器永遠都不能建立這個確切的數組。
建立一個Object數組,將其轉型爲所但願的數組類型。這是能夠編譯的,可是不能運用。
public class ArrayOfGeneric { static final int SIZE = 100; static Generic<Integer>[] gia; @SuppressWarnings("unchecked") public static void main(String[] args) { // Compiles; produces ClassCastException: // gia = (Generic<Integer>[])new Object[SIZE]; //運行期報錯 // Runtime type is the raw (erased) type: gia = (Generic<Integer>[])new Generic[SIZE]; System.out.println(gia.getClass().getSimpleName()); gia[0] = new Generic<Integer>(); //! gia[1] = new Object(); // Compile-time error // Discovers type mismatch at compile time: //! gia[2] = new Generic<Double>(); } } /* Output: Generic[] *///:~
成功建立泛型數組惟一方式就是建立一個被擦除類型的新數組,而後對其轉型:
public class GenericArray<T> { private T[] array; @SuppressWarnings("unchecked") public GenericArray(int sz) { array = (T[])new Object[sz]; }//建立Object轉型爲T public void put(int index, T item) { array[index] = item; } public T get(int index) { return array[index]; } // Method that exposes the underlying representation: public T[] rep() { return array; } public static void main(String[] args) { GenericArray<Integer> gai = new GenericArray<Integer>(10); // This causes a ClassCastException: //! Integer[] ia = gai.rep(); // This is OK: Object[] oa = gai.rep(); } } ///:~
不能聲明T[] array=new T[sz],所以咱們建立了一個對象數組,而後將其轉型。
由於有了擦除,數組的運行時類型就只能是Object[]。若是咱們當即將其轉型爲T[]那麼再編譯器該數組的實際類型就將丟失,而編譯器可能會錯過某些潛在的錯誤檢查。由於這樣,最好再集合內部使用Object[],而後當你使用數組元素時,添加一個對T的轉型。
public class GenericArray2<T> { private Object[] array; public GenericArray2(int sz) { array = new Object[sz]; } public void put(int index, T item) { array[index] = item; } @SuppressWarnings("unchecked") public T get(int index) { return (T)array[index]; } @SuppressWarnings("unchecked") public T[] rep() { return (T[])array; // Warning: unchecked cast } public static void main(String[] args) { GenericArray2<Integer> gai = new GenericArray2<Integer>(10); for(int i = 0; i < 10; i ++) gai.put(i, i); for(int i = 0; i < 10; i ++) System.out.print(gai.get(i) + " "); System.out.println(); try { Integer[] ia = gai.rep(); } catch(Exception e) { System.out.println(e); } } } /* Output: (Sample) 0 1 2 3 4 5 6 7 8 9 java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer; *///:~
在內部將array看成Object[]而不是T[]處理的優點:咱們不太可能忘記這個數組的運行時類型,從而意外地引入缺陷。
對於新代碼,應該傳遞一個類型標識:
import java.lang.reflect.*; public class GenericArrayWithTypeToken<T> { private T[] array; @SuppressWarnings("unchecked") public GenericArrayWithTypeToken(Class<T> type, int sz) { array = (T[])Array.newInstance(type, sz); } public void put(int index, T item) { array[index] = item; } public T get(int index) { return array[index]; } // Expose the underlying representation: public T[] rep() { return array; } public static void main(String[] args) { GenericArrayWithTypeToken<Integer> gai = new GenericArrayWithTypeToken<Integer>( Integer.class, 10); // This now works: for(int i=0;i<10;i++) gai.put(i,i); Integer[] ia = gai.rep(); for (int i:ia ) System.out.println(i); } } ///:~
類型標記Class< T >被傳遞到構造器中,以便從擦除中恢復,使得咱們能夠建立須要得實際類型得數組。
邊界使得你能夠在用於泛型得參數類型上設置限制條件。儘管這使得你能夠強制規定泛型能夠應用得類型,可是其潛在得一個更重要得效果是你能夠按照本身的邊界類型來調用方法。
由於擦除移除了類型信息,因此,能夠i用無界泛型參數調用的方法只是那些能夠用Object調用的方法。若是可以將這個參數限制爲某個類型子集,那麼你就能夠用這些類型子集來調用方法。爲了執行這種限制,Java泛型重用了extends關鍵字。
interface HasColor { java.awt.Color getColor(); } class Colored<T extends HasColor> { T item; Colored(T item) { this.item = item; } T getItem() { return item; } // The bound allows you to call a method: java.awt.Color color() { return item.getColor(); } } class Dimension { public int x, y, z; } // This won't work -- class must be first, then interfaces: // class ColoredDimension<T extends HasColor & Dimension> { // Multiple bounds: class ColoredDimension<T extends Dimension & HasColor> { T item; ColoredDimension(T item) { this.item = item; } T getItem() { return item; } java.awt.Color color() { return item.getColor(); } int getX() { return item.x; } int getY() { return item.y; } int getZ() { return item.z; } } interface Weight { int weight(); } // As with inheritance, you can have only one // concrete class but multiple interfaces: class Solid<T extends Dimension & HasColor & Weight> { T item; Solid(T item) { this.item = item; } T getItem() { return item; } java.awt.Color color() { return item.getColor(); } int getX() { return item.x; } int getY() { return item.y; } int getZ() { return item.z; } int weight() { return item.weight(); } } class Bounded extends Dimension implements HasColor, Weight { public java.awt.Color getColor() { return null; } public int weight() { return 0; } } public class BasicBounds { public static void main(String[] args) { Solid<Bounded> solid = new Solid<Bounded>(new Bounded()); solid.color(); solid.getY(); solid.weight(); } } ///:~
經過繼承將邊界傳遞下去:
class HoldItem<T> { T item; HoldItem(T item) { this.item = item; } T getItem() { return item; } } class Colored2<T extends HasColor> extends HoldItem<T> { Colored2(T item) { super(item); } java.awt.Color color() { return item.getColor(); } } class ColoredDimension2<T extends Dimension & HasColor> extends Colored2<T> { ColoredDimension2(T item) { super(item); } int getX() { return item.x; } int getY() { return item.y; } int getZ() { return item.z; } } class Solid2<T extends Dimension & HasColor & Weight> extends ColoredDimension2<T> { Solid2(T item) { super(item); } int weight() { return item.weight(); } } public class InheritBounds { public static void main(String[] args) { Solid2<Bounded> solid2 = new Solid2<Bounded>(new Bounded()); solid2.color(); solid2.getY(); solid2.weight(); } } ///:~
更多層次的繼承:
// Demonstrating bounds in Java generics. import java.util.*; interface SuperPower {} interface XRayVision extends SuperPower { void seeThroughWalls(); } interface SuperHearing extends SuperPower { void hearSubtleNoises(); } interface SuperSmell extends SuperPower { void trackBySmell(); } class SuperHero<POWER extends SuperPower> { POWER power; SuperHero(POWER power) { this.power = power; } POWER getPower() { return power; } } class SuperSleuth<POWER extends XRayVision>//POWER邊界來源於XRayVision的父類 extends SuperHero<POWER> { SuperSleuth(POWER power) { super(power); } void see() { power.seeThroughWalls(); } } class CanineHero<POWER extends SuperHearing & SuperSmell> extends SuperHero<POWER> { CanineHero(POWER power) { super(power); } void hear() { power.hearSubtleNoises(); } void smell() { power.trackBySmell(); } } class SuperHearSmell implements SuperHearing, SuperSmell { public void hearSubtleNoises() {} public void trackBySmell() {} } class DogBoy extends CanineHero<SuperHearSmell> { DogBoy() { super(new SuperHearSmell()); } } public class EpicBattle { // Bounds in generic methods: static <POWER extends SuperHearing> void useSuperHearing(SuperHero<POWER> hero) { hero.getPower().hearSubtleNoises(); } static <POWER extends SuperHearing & SuperSmell> void superFind(SuperHero<POWER> hero) { hero.getPower().hearSubtleNoises(); hero.getPower().trackBySmell(); } public static void main(String[] args) { DogBoy dogBoy = new DogBoy(); useSuperHearing(dogBoy); superFind(dogBoy); // You can do this: List<? extends SuperHearing> audioBoys; // But you can't do this: // List<? extends SuperHearing & SuperSmell> dogBoys; } } ///:~
能夠嚮導出類型的數組賦予基類型的數組引用:
class Fruit {} class Apple extends Fruit {} class Jonathan extends Apple {} class Orange extends Fruit {} public class CovariantArrays { public static void main(String[] args) { Fruit[] fruit = new Apple[10]; fruit[0] = new Apple(); // OK fruit[1] = new Jonathan(); // OK // Runtime type is Apple[], not Fruit[] or Orange[]: try { // Compiler allows you to add Fruit: fruit[2] = new Fruit(); // ArrayStoreException 沒法向上轉型 } catch(Exception e) { System.out.println(e); } try { // Compiler allows you to add Oranges: fruit[3] = new Orange(); // ArrayStoreException } catch(Exception e) { System.out.println(e); } } } /* Output: java.lang.ArrayStoreException: Fruit java.lang.ArrayStoreException: Orange *///:~
數組對象能夠保留有關它們包含的對象類型的規則。
import java.util.*; public class NonCovariantGenerics { // Compile Error: incompatible types: List<Fruit> flist = new ArrayList<Apple>();//不能把一個Apple容器賦值給一個Fruit容器 } ///:~
於數組不一樣,泛型沒有內建的協變類型。有時你想要在兩個類型之間創建某種類型的向上轉型關係,這正是通配符所容許的:
import java.util.*; public class GenericsAndCovariance { public static void main(String[] args) { // Wildcards allow covariance: List<? extends Fruit> flist = new ArrayList<Apple>();//這裏將向上轉型將丟失向裏傳遞任何對象的能力,以致於沒法傳入任何類型,Object也不能夠傳入。 // Compile Error: can't add any type of object: // flist.add(new Apple()); // 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); } } ///:~
import java.util.*; public class CompilerIntelligence { public static void main(String[] args) { List<? extends Fruit> flist = Arrays.asList(new Apple()); Apple a = (Apple)flist.get(0); // No warning flist.contains(new Apple()); // 對象參數是Object flist.indexOf(new Apple()); // Argument is 'Object' //flist.add(new Apple());這裏Add須要的參數是? extends Fruit,可是它並不知道須要那個具體的類型,全部它不會接受任何參數 } } ///:~
add()將接受一個具備泛型參數類型的參數,可是contains()和indexOf()將接受Object類型的參數,所以當你指定一個ArrayList<? extends Fruit>時,add()的參數就變成了? extends Fruit。
public class Holder<T> { private T value; public Holder() {} public Holder(T val) { value = val; } public void set(T val) { value = val; } public T get() { return value; } public boolean equals(Object obj) { return value.equals(obj); } public static void main(String[] args) { Holder<Apple> Apple = new Holder<Apple>(new Apple()); Apple d = Apple.get(); Apple.set(d); // Holder<Fruit> Fruit = Apple; // Cannot upcast Holder<? extends Fruit> fruit = Apple; // OK Fruit p = fruit.get(); d = (Apple)fruit.get(); // Returns 'Object' try { Orange c = (Orange)fruit.get(); // No warning } catch(Exception e) { System.out.println(e); } // fruit.set(new Apple()); // Cannot call set() // fruit.set(new Fruit()); // Cannot call set() System.out.println(fruit.equals(d)); // OK } } /* Output: (Sample) java.lang.ClassCastException: Apple cannot be cast to Orange true *///:~
超類型通配符,能夠聲明通配符是由某個特定類的任何基類來界定的,方法是指定<? super MyClass>,甚至使用類型參數<? super T>。
import java.util.*; public class SuperTypeWildcards { static void writeTo(List<? super Apple> apples) { apples.add(new Apple()); apples.add(new Jonathan()); // apples.add(new Fruit()); // Error } } ///:~
超類型邊界放鬆了在能夠向方法傳遞的參數上所作的限制:
import java.util.*; public class GenericWriting { static <T> void writeExact(List<T > list, T item) { list.add(item); } static List<Apple> apples = new ArrayList<Apple>(); static List<Fruit> fruit = new ArrayList<Fruit>(); static void f1() { writeExact(apples, new Apple()); // writeExact(fruit, new Apple()); // Error: // Incompatible types: found Fruit, required 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(); } } ///:~
無界通配符< ? >看起來意味着任何事物,所以使用無界通配符好像等價於使用原生類型。
import java.util.*; public class UnboundedWildcards1 { static List list1; static List<?> list2; static List<? extends Object> list3; static void assign1(List list) { list1 = list; list2 = list; // list3 = list; // Warning: unchecked conversion // Found: List, Required: List<? extends Object> } static void assign2(List<?> list) { list1 = list; list2 = list; list3 = list; } static void assign3(List<? extends Object> list) { list1 = list; list2 = list; list3 = list; } public static void main(String[] args) { assign1(new ArrayList()); assign2(new ArrayList()); // assign3(new ArrayList()); // Warning: // Unchecked conversion. Found: ArrayList // Required: List<? extends Object> assign1(new ArrayList<String>()); assign2(new ArrayList<String>()); assign3(new ArrayList<String>()); // Both forms are acceptable as List<?>: List<?> wildList = new ArrayList(); wildList = new ArrayList<String>(); assign1(wildList); assign2(wildList); assign3(wildList); } } ///:~
< ? >能夠被認爲是一種裝飾,實際上,它是在聲明:我是想用Java的泛型來編寫這段代碼,我在這裏並非要用原生類型,可是在當前這種狀況下,泛型參數能夠持有任何類型。
import java.util.*; public class UnboundedWildcards2 { static Map map1; static Map<?,?> map2; static Map<String,?> map3; static void assign1(Map map) { map1 = map; } static void assign2(Map<?,?> map) { map2 = map; } static void assign3(Map<String,?> map) { map3 = map; } public static void main(String[] args) { assign1(new HashMap()); assign2(new HashMap()); assign3(new HashMap()); // Warning: // Unchecked conversion. Found: HashMap // Required: Map<String,?> assign1(new HashMap<String,Integer>()); assign2(new HashMap<String,Integer>()); assign3(new HashMap<String,Integer>()); } } ///:~
事實上,因爲泛型參數將擦出到它的第一個邊界,所以LIst<?>看起來等價於LIst< Object >,而List實際上也是List< Object >——除非這些語句都不爲真。List實際上表示持有任何Object類型的原生List,而List<?>表示具備某種特定類型的非原生List。
//: generics/Wildcards.java // Exploring the meaning of wildcards. public class Wildcards { // Raw argument: static void rawArgs(Holder holder, Object arg) { holder.set(arg); // Warning:編譯器知道Holder是泛型,所以向set傳遞Object是不安全的 // Unchecked call to set(T) as a // member of the raw type Holder // holder.set(new Wildcards()); // Same warning // Can't do this; don't have any 'T': // T t = holder.get(); // OK, but type information has been lost: Object obj = holder.get(); } // Similar to rawArgs(), but errors instead of warnings: static void unboundedArg(Holder<?> holder, Object arg) { // holder.set(arg); // Error:原生類型將持有任何類型的組合,但Holder<?>將持有具備某種具體類型的同構集合,所以,不能向其中傳遞Object。 // set(capture of ?) in Holder<capture of ?> // cannot be applied to (Object) // holder.set(new Wildcards()); // Same error // Can't do this; don't have any 'T': // T t = holder.get(); // OK, but type information has been lost: Object obj = holder.get(); } static <T> T exact1(Holder<T> holder) { T t = holder.get(); return t; } static <T> T exact2(Holder<T> holder, T arg) { holder.set(arg); T t = holder.get(); return t; } static <T> T wildSubtype(Holder<? extends T> holder, T arg) { // holder.set(arg); // Error:將不容許傳入任何類型,防止傳入不一樣類型結構的集合。 // set(capture of ? extends T) in // Holder<capture of ? extends T> // cannot be applied to (T) T t = holder.get(); return t; } static <T> void wildSupertype(Holder<? super T> holder, T arg) { holder.set(arg);//超類,任何繼承自T類型對象均可以傳入 // T t = holder.get(); // Error: // Incompatible types: found Object, required T // OK, but type information has been lost: Object obj = holder.get();//這裏傳出的類型能夠是任何超類型,因此這裏使用Object纔是安全的。 } public static void main(String[] args) { Holder raw = new Holder<Long>(); // Or: raw = new Holder(); Holder<Long> qualified = new Holder<Long>(); Holder<?> unbounded = new Holder<Long>(); Holder<? extends Long> bounded = new Holder<Long>(); Long lng = 1L; rawArgs(raw, lng); rawArgs(qualified, lng); rawArgs(unbounded, lng); rawArgs(bounded, lng); unboundedArg(raw, lng); unboundedArg(qualified, lng); unboundedArg(unbounded, lng); unboundedArg(bounded, lng); Object r1 = exact1(raw); // Warnings: // Unchecked conversion from Holder to Holder<T> // Unchecked method invocation: exact1(Holder<T>) // is applied to (Holder) Long r2 = exact1(qualified); Object r3 = exact1(unbounded); // Must return Object Long r4 = exact1(bounded); Long r5 = exact2(raw, lng); // Warnings: // Unchecked conversion from Holder to Holder<Long> // Unchecked method invocation: exact2(Holder<T>,T) // is applied to (Holder,Long) Long r6 = exact2(qualified, lng); // Long r7 = exact2(unbounded, lng); // Error: // exact2(Holder<T>,T) cannot be applied to // (Holder<capture of ?>,Long) // Long r8 = exact2(bounded, lng); // Error: // exact2(Holder<T>,T) cannot be applied // to (Holder<capture of ? extends Long>,Long) // Long r9 = wildSubtype(raw, lng); // Warnings: // Unchecked conversion from Holder // to Holder<? extends Long> // Unchecked method invocation: // wildSubtype(Holder<? extends T>,T) is // applied to (Holder,Long) Long r10 = wildSubtype(qualified, lng); // OK, but can only return Object: Object r11 = wildSubtype(unbounded, lng); Long r12 = wildSubtype(bounded, lng); // wildSupertype(raw, lng); // Warnings: // Unchecked conversion from Holder // to Holder<? super Long> // Unchecked method invocation: // wildSupertype(Holder<? super T>,T) // is applied to (Holder,Long) wildSupertype(qualified, lng); // wildSupertype(unbounded, lng); // Error: // wildSupertype(Holder<? super T>,T) cannot be // applied to (Holder<capture of ?>,Long) // wildSupertype(bounded, lng); // Error: // wildSupertype(Holder<? super T>,T) cannot be // applied to (Holder<capture of ? extends Long>,Long) } } ///:~
使用確切類型來替代通配符類型的好處是,能夠用泛型參數來作更多的事,可是使用通配符使得你必須接收範圍更寬的參數化類型做爲參數。
有一個狀況特定須要使用<?>而不是原生類型。若是向一個使用<?>方法傳遞原生類型,那麼對編譯器來講,可能會推斷出實際類型,使得這個方法能夠迴轉並調用另外一個使用這個確切類型的方法,它被稱爲:捕獲轉換。由於未指定的通配符類型被捕獲,並被轉換爲確切類型。
public class CaptureConversion { static <T> void f1(Holder<T> holder) {//參數肯定,沒有通配符或邊界 T t = holder.get(); System.out.println(t.getClass().getSimpleName()); } static void f2(Holder<?> holder) {//無界通配符,類型是未知的,可是這裏調用了f1.這裏參數類型在調用f2的過程當中被捕獲,所以它能夠在對f1的調用中被使用。 f1(holder); // Call with captured type } @SuppressWarnings("unchecked") public static void main(String[] args) { Holder raw = new Holder<Integer>(1); f1(raw); // Produces warnings f2(raw); // No warnings Holder rawBasic = new Holder(); rawBasic.set(new Object()); // Warning f2(rawBasic); // No warnings // Upcast to Holder<?>, still figures it out: Holder<?> wildcarded = new Holder<Double>(1.0); f2(wildcarded); } } /* Output: Integer Object Double *///:~
使用基本類型的包裝器:
import java.util.*; public class ListOfInt { public static void main(String[] args) { List<Integer> li = new ArrayList<Integer>(); for(int i = 0; i < 5; i++) li.add(i); for(int i : li) System.out.print(i + " "); } } /* Output: 0 1 2 3 4 *///:~
若是對性能有要求,可使用專門的適配基本類型的容器版本:Org.apache.commons.collections.primitives。
使用另外一種方法建立Byte的Set:
import java.util.*; public class ByteSet { Byte[] possibles = { 1,2,3,4,5,6,7,8,9 }; Set<Byte> mySet = new HashSet<Byte>(Arrays.asList(possibles)); // But you can't do this: // Set<Byte> mySet2 = new HashSet<Byte>( // Arrays.<Byte>asList(1,2,3,4,5,6,7,8,9)); } ///:~
使用泛型Generator接口:
import net.mindview.util.*; // Fill an array using a generator: class FArray { public static <T> T[] fill(T[] a, Generator<T> gen) { for(int i = 0; i < a.length; i++) a[i] = gen.next(); return a; } } public class PrimitiveGenericTest { public static void main(String[] args) { String[] strings = FArray.fill( new String[8], new RandomGenerator.String()); for(String s : strings) System.out.println(s); Integer[] integers = FArray.fill( new Integer[7], new RandomGenerator.Integer()); for(int i: integers) System.out.println(i); // Autoboxing won't save you here. This won't compile: // int[] b = // FArray.fill(new int[7], new RandIntGenerator()); } } /* Output: YNzbrnyGcF OWZnTcQrGs eGZMmJMRoE suEcUOneOE dLsmwHLGEa hKcxrEqUCB bkInaMesbt 7052 6665 2654 3909 5202 2209 5458 *///:~
一個類不能實現同一個泛型接口的兩種變體,因爲擦除的緣由,這兩個變體會成爲相同的接口。
interface Payable<T> {} class Employee1 implements Payable<Employee1> {} class Hourly extends Employee implements Payable<Hourly> {} ///:~這裏都被擦除爲Object public class MultipleInterfaceVariants{ public static void main(String[] args){ } }
使用帶有泛型類型參數的轉型或instanceof不會有任何效果。
下面的容器在內部將各個值存儲爲Object,並在獲取這些值的時候,再將它們轉型回T:
class FixedSizeStack<T> { private int index = 0; private Object[] storage; public FixedSizeStack(int size) { storage = new Object[size]; } public void push(T item) { storage[index++] = item; } @SuppressWarnings("unchecked") public T pop() { return (T)storage[--index]; }//因爲擦除的緣由,編譯器沒法知道這個轉型是否安全的,而且pop()方法實際上並無執行任何轉型。 } public class GenericCast { public static final int SIZE = 10; public static void main(String[] args) { FixedSizeStack<String> strings = new FixedSizeStack<String>(SIZE); for(String s : "A B C D E F G H I J".split(" ")) strings.push(s); for(int i = 0; i < SIZE; i++) { String s = strings.pop(); System.out.print(s + " "); } } } /* Output: J I H G F E D C B A *///:~
泛型沒有消除對轉型的須要,這就會由編譯器產生警告,而這個警告是不恰當的:
import java.io.*; import java.util.*; public class NeedCasting { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { ObjectInputStream in = new ObjectInputStream( new FileInputStream("")); List<Widget> shapes = (List<Widget>)in.readObject();//readObject沒法知道它正在讀取的是什麼,所以它返回的是必須轉型的對象 } } ///:~
泛型類來轉型:
import java.io.*; import java.util.*; public class ClassCasting { @SuppressWarnings("unchecked") public void f(String[] args) throws Exception { ObjectInputStream in = new ObjectInputStream( new FileInputStream(args[0])); // Won't Compile: // List<Widget> lw1 = // List<Widget>.class.cast(in.readObject()); List<Widget> lw2 = List.class.cast(in.readObject()); } } ///:~
下面的程序不能編譯:
import java.util.*; public class UseList<W,T> { void f1(List<T> v) {}//重載將產生相同的類型簽名 void f1(List<W> v) {} } ///:~
建立一個於其餘對象比較類:
public class ComparablePet implements Comparable<ComparablePet> { public int compareTo(ComparablePet arg) { return 0; } } ///:~
對其子類進行窄化沒有意義:代碼是不能運行的
class Cat extends ComparablePet implements Comparable<Cat>{ // Error: Comparable cannot be inherited with // different arguments: <Cat> and <Pet> public int compareTo(Cat arg) { return 0; } } ///:~
下面代碼確是能夠運行的,只要它們精確的類相同:
class Hamster extends ComparablePet implements Comparable<ComparablePet> { public int compareTo(ComparablePet arg) { return 0; } } // Or just: class Gecko extends ComparablePet { public int compareTo(ComparablePet arg) { return 0; } } ///:~
在Java泛型中,有一個好像是常常性出現的慣用法:
class SelfBounded<T extends SelfBounded<T>>
這就像兩面鏡子彼此照向對方引發目眩效果,是一種無限反射。SelfBounded類接受泛型參數T,而T由一個邊界類限定,這個邊界就是擁有T做爲參數的SelfBounded。
不能直接繼承一個泛型參數,可是,能夠繼承在其本身的定義中使用這個泛型參數的類。
class GenericType<T> {} public class CuriouslyRecurringGeneric extends GenericType<CuriouslyRecurringGeneric> {} ///:~
這被稱爲古怪的循環泛型。
Java中的泛型關乎參數和返回類型,所以它可以產生使用導出類做爲其參數和返回類型的基類。它還能將導出類型用做其域類型,甚至那些將被擦除爲Object的類型。
public class BasicHolder<T> { T element; void set(T arg) { element = arg; } T get() { return element; } void f() { System.out.println(element.getClass().getSimpleName()); } } ///:~
這是一個普通的泛型類型,下面再一個古怪的循環泛型中使用它:
class Subtype extends BasicHolder<Subtype> {} public class CRGWithBasicHolder { public static void main(String[] args) { Subtype st1 = new Subtype(), st2 = new Subtype(); st1.set(st2); Subtype st3 = st1.get(); st1.f(); } } /* Output: Subtype *///:~
CRG本質:基類用導出類替代其參數
在所產生的類中將使用確切類型而不是基類型。
BasicHolder可使用任何類型做爲其泛型參數:
class Other {} class BasicOther extends BasicHolder<Other> {} public class Unconstrained { public static void main(String[] args) { BasicOther b = new BasicOther(), b2 = new BasicOther(); b.set(new Other()); Other other = b.get(); b.f(); } } /* Output: Other *///:~
自限定將採起額外的步驟,強制泛型看成其本身的邊界參數來使用:
class SelfBounded<T extends SelfBounded<T>> { T element; SelfBounded<T> set(T arg) { element = arg; return this; } T get() {System.out.println(this.getClass().getSimpleName()); return element; } } class A extends SelfBounded<A> {}//強制要求將正在定義的類看成參數傳遞給基類。 class B extends SelfBounded<A> {} // Also OK class C extends SelfBounded<C> { C setAndGet(C arg) { set(arg); return get(); } } class D {} // Can't do this: //class E extends SelfBounded<D> {} // Compile error: Type parameter D is not within its bound // Alas, you can do this, so you can't force the idiom: class F extends SelfBounded {}//自限定慣用法不是可強制執行的 public class SelfBounding { public static void main(String[] args) { A a = new A(); a.set(new A()); a = a.set(new A()).get(); a = a.get(); C c = new C(); c = c.setAndGet(new C()); } } ///:~
自限定參數意義:它能夠保證類型參數必須與正在被定義的類相同。
能夠移除自限定這個限制,這樣全部的類仍舊是能夠編譯的。
public class NotSelfBounded<T> { T element; NotSelfBounded<T> set(T arg) { element = arg; return this; } T get() { return element; } } class A2 extends NotSelfBounded<A2> {} class B2 extends NotSelfBounded<A2> {} class C2 extends NotSelfBounded<C2> { C2 setAndGet(C2 arg) { set(arg); return get(); } } class D2 {} // Now this is OK: class E2 extends NotSelfBounded<D2> {} ///:~
很明顯,自限定限制只能強制做用於繼承關係,若是使用自限定,就應該瞭解這個類所用的類型參數將與使用這個參數的類具備相同的基類型。
還能夠將自限定用於泛型方法:
public class SelfBoundingMethods { static <T extends SelfBounded<T>> T f(T arg) { return arg.set(arg).get(); } public static void main(String[] args) { A a = f(new A()); } } ///:~
自限定類型的價值在於它們能夠產生協變參數類型——方法參數類型會隨子類而變化。
class Base {} class Derived extends Base {} interface OrdinaryGetter { Base get(); } interface DerivedGetter extends OrdinaryGetter { // Return type of overridden method is allowed to vary: Derived get(); } public class CovariantReturnTypes { void test(DerivedGetter d) { Derived d2 = d.get(); } } ///:~
自限定泛型事實上將產生確切的導出類型做爲其返回值:
interface GenericGetter<T extends GenericGetter<T>> { T get(); } interface Getter extends GenericGetter<Getter> {} public class GenericsAndReturnTypes { void test(Getter g) { Getter result = g.get(); GenericGetter gg = g.get(); // Also the base type } } ///:~
在使用自限定類型時,在導出類只有一個方法,而且這個方法接收導出類型而不是基類型爲參數:
interface SelfBoundSetter<T extends SelfBoundSetter<T>> { void set(T arg); } interface Setter extends SelfBoundSetter<Setter> {} public class SelfBoundingAndCovariantArguments { void testA(Setter s1, Setter s2, SelfBoundSetter sbs) { s1.set(s2); // s1.set(sbs); // Error:編譯器不能識別將基類型看成參數傳遞給set()的嘗試,由於沒有任何方法具備這樣的簽名。實際上,這個參數已經被覆蓋了 // set(Setter) in SelfBoundSetter<Setter> // cannot be applied to (SelfBoundSetter) } } ///:~
若是不使用自限定類型,普通的繼承機制就會介入,而將可以重載:
class GenericSetter<T> { // Not self-bounded void set(T arg){ System.out.println("GenericSetter.set(Base)"); } } class DerivedGS extends GenericSetter<Base> { void set(Derived derived){ System.out.println("DerivedGS.set(Derived)"); } } public class PlainGenericInheritance { public static void main(String[] args) { Base base = new Base(); Derived derived = new Derived(); DerivedGS dgs = new DerivedGS(); dgs.set(derived); dgs.set(base); // Compiles: overloaded, not overridden! } } /* Output: DerivedGS.set(Derived) GenericSetter.set(Base) *///:~
若是不使用自限定,將重載參數類型。若是使用自限定,只能得到某個方法的一個版本,它將接收確切的參數類型。
Java SE5的java.util.Collections中有一組便利工具,它們是:靜態方法checkedCollection(),checkedList(),checkedMap(),checkedSet(),checkedSortedMap()和checkedSortedSet()。這些方法每個都會將你但願動態檢查的容器做爲一個參數接受,並將你但願強制要求的類型做爲第二個參數接受。
import pets.*; import java.util.*; public class CheckedList { @SuppressWarnings("unchecked") static void oldStyleMethod(List probablyDogs) { probablyDogs.add(new Cat()); } public static void main(String[] args) { List<Dog> dogs1 = new ArrayList<Dog>(); oldStyleMethod(dogs1); // Quietly accepts a Cat List<Dog> dogs2 = Collections.checkedList( new ArrayList<Dog>(), Dog.class); try { oldStyleMethod(dogs2); // Throws an exception將拋出異常 } catch(Exception e) { System.out.println(e); } // Derived types work fine: List<Pet> pets = Collections.checkedList(//導出類型放到基類型中檢查是沒有關係的 new ArrayList<Pet>(), Pet.class); pets.add(new Dog()); pets.add(new Cat()); } } /* Output: java.lang.ClassCastException: Attempt to insert class typeinfo.pets.Cat element into collection with element type class typeinfo.pets.Dog *///:~
因爲擦除的緣由,將泛型應用於異常是很是受限的。catch語句不能捕獲泛型類型的異常,由於編譯期和運行時都必須知道異常的確切類型,泛型類也不能直接或間接繼承自Throwable。
import java.util.*; interface Processor<T,E extends Exception> { void process(List<T> resultCollector) throws E; } class ProcessRunner<T,E extends Exception> extends ArrayList<Processor<T,E>> { List<T> processAll() throws E { List<T> resultCollector = new ArrayList<T>(); for(Processor<T,E> processor : this) processor.process(resultCollector); return resultCollector; } } class Failure1 extends Exception {} class Processor1 implements Processor<String,Failure1> { static int count = 3; public void process(List<String> resultCollector) throws Failure1 { if(count-- > 1) resultCollector.add("Hep!"); else resultCollector.add("Ho!"); if(count < 0) throw new Failure1(); } } class Failure2 extends Exception {} class Processor2 implements Processor<Integer,Failure2> { static int count = 2; public void process(List<Integer> resultCollector) throws Failure2 { if(count-- == 0) resultCollector.add(47); else { resultCollector.add(11); } if(count < 0) throw new Failure2(); } } public class ThrowGenericException { public static void main(String[] args) { ProcessRunner<String,Failure1> runner = new ProcessRunner<String,Failure1>(); for(int i = 0; i < 3; i++) runner.add(new Processor1()); try { System.out.println(runner.processAll()); } catch(Failure1 e) { System.out.println(e); } ProcessRunner<Integer,Failure2> runner2 = new ProcessRunner<Integer,Failure2>(); for(int i = 0; i < 3; i++) runner2.add(new Processor2()); try { System.out.println(runner2.processAll()); } catch(Failure2 e) { System.out.println(e); } } } ///:~
基本概念:混合多個類的能力,以產生一個能夠表示混型中全部類型的類。
混型的價值之一是它們能夠將特性和行爲一致地應用於多個類之上。若是想在混型中修改某些東西,做爲一中意外的好處,這些修改將會應用於混型所應用的全部類型之上。
import java.util.*; interface TimeStamped { long getStamp(); } class TimeStampedImp implements TimeStamped { private final long timeStamp; public TimeStampedImp() { timeStamp = new Date().getTime(); } public long getStamp() { return timeStamp; } } interface SerialNumbered { long getSerialNumber(); } class SerialNumberedImp implements SerialNumbered { private static long counter = 1; private final long serialNumber = counter++; public long getSerialNumber() { return serialNumber; } } interface Basic { public void set(String val); public String get(); } class BasicImp implements Basic { private String value; public void set(String val) { value = val; } public String get() { return value; } } class Mixin extends BasicImp implements TimeStamped, SerialNumbered {//使用代理,每一個混入類型都要求在Mixin中有一個相應的域,必須在Mixin中編寫必須的方法,將方法調用轉發給恰當的對象 private TimeStamped timeStamp = new TimeStampedImp(); private SerialNumbered serialNumber = new SerialNumberedImp(); public long getStamp() { return timeStamp.getStamp(); } public long getSerialNumber() { return serialNumber.getSerialNumber(); } } public class Mixins { public static void main(String[] args) { Mixin mixin1 = new Mixin(), mixin2 = new Mixin(); mixin1.set("test string 1"); mixin2.set("test string 2"); System.out.println(mixin1.get() + " " + mixin1.getStamp() + " " + mixin1.getSerialNumber()); System.out.println(mixin2.get() + " " + mixin2.getStamp() + " " + mixin2.getSerialNumber()); } } /* Output: (Sample) test string 1 1132437151359 1 test string 2 1132437151359 2 *///:~
觀察混型的使用方式時,就會發現混型概念好像與裝飾器設計模式很像。裝飾器常常用於知足各類可能的組合,而直接子類化會產生過多的類,所以不實際。
裝飾器模式使用分層對象來動態透明地想單個對象中添加責任。裝飾器指定包裝在最初的對象周圍的全部對象都具備相同的基本接口。某些事物是可裝飾的,能夠經過將其餘類包裝在這個可裝飾對象的周圍,來將功能分層。
裝飾器是經過使用組合和形式化結構來實現的,而混型是基於繼承的。
將前面的示例改寫爲使用裝飾器:
package decorator; import java.util.*; class Basic { private String value; public void set(String val) { value = val; } public String get() { return value; } } class Decorator extends Basic { protected Basic basic; public Decorator(Basic basic) { this.basic = basic; } public void set(String val) { basic.set(val); } public String get() { return basic.get(); } } class TimeStamped extends Decorator { private final long timeStamp; public TimeStamped(Basic basic) { super(basic); timeStamp = new Date().getTime(); } public long getStamp() { return timeStamp; } } class SerialNumbered extends Decorator { private static long counter = 1; private final long serialNumber = counter++; public SerialNumbered(Basic basic) { super(basic); } public long getSerialNumber() { return serialNumber; } } public class Decoration { public static void main(String[] args) { TimeStamped t = new TimeStamped(new Basic()); TimeStamped t2 = new TimeStamped( new SerialNumbered(new Basic())); //! t2.getSerialNumber(); // Not available SerialNumbered s = new SerialNumbered(new Basic()); SerialNumbered s2 = new SerialNumbered( new TimeStamped(new Basic())); //! s2.getStamp(); // Not available } } ///:~
使用裝飾器所產生的對象類型是最後被裝飾的類型。也就是說,儘管能夠添加多個層,可是最後一層纔是實際類型,所以只有最後一層的方法是可視的,而混型的類型是全部被混合到一塊兒的類型。
可使用動態代理來建立一種比裝飾器更貼近混型的模型機制。經過使用動態代理,所產生的類的動態類型將會是已經混入的組合類型。
import java.lang.reflect.*; import java.util.*; import net.mindview.util.*; import static net.mindview.util.Tuple.*; class MixinProxy implements InvocationHandler { Map<String,Object> delegatesByMethod; public MixinProxy(TwoTuple<Object,Class<?>>... pairs) { delegatesByMethod = new HashMap<String,Object>(); for(TwoTuple<Object,Class<?>> pair : pairs) { for(Method method : pair.second.getMethods()) { String methodName = method.getName(); // The first interface in the map // implements the method. if (!delegatesByMethod.containsKey(methodName)) delegatesByMethod.put(methodName, pair.first); } } } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); Object delegate = delegatesByMethod.get(methodName); return method.invoke(delegate, args); } @SuppressWarnings("unchecked") public static Object newInstance(TwoTuple... pairs) { Class[] interfaces = new Class[pairs.length]; for(int i = 0; i < pairs.length; i++) { interfaces[i] = (Class)pairs[i].second; } ClassLoader cl = pairs[0].first.getClass().getClassLoader(); return Proxy.newProxyInstance( cl, interfaces, new MixinProxy(pairs)); } } public class DynamicProxyMixin { public static void main(String[] args) { Object mixin = MixinProxy.newInstance( tuple(new BasicImp(), Basic.class), tuple(new TimeStampedImp(), TimeStamped.class), tuple(new SerialNumberedImp(),SerialNumbered.class)); Basic b = (Basic)mixin; TimeStamped t = (TimeStamped)mixin; SerialNumbered s = (SerialNumbered)mixin; b.set("Hello"); System.out.println(b.get()); System.out.println(t.getStamp()); System.out.println(s.getSerialNumber()); } } /* Output: (Sample) Hello 1132519137015 1 *///:~
當要在泛型類型上執行操做時,就會產生問題,由於擦除要求指定可能會用到泛型類型的邊界,以安全的調用代碼中的泛型對象上具體方法。這是對泛化的一種明顯限制,由於必須限制你的泛型類型,使它們繼承自特定的類,或者實現特定接口。某些狀況下,你最終可能會使用普通類或普通接口,由於限定邊界的泛型可能會和指定類或接口沒有任何區別。
某些變成語言提供的一種解決方案稱爲潛在類型機制或結構化類型機制。
潛在類型機制使得你能夠橫跨類繼承結構,調用不屬於某個公共接口的方法。所以,實際上一段代碼能夠聲明:我不關心你是什麼類型,只要你能夠speak()和sit()便可呃。因爲不要求具體類型,所以代碼能夠更加泛化。
潛在機制一種代碼組織和複用機制
潛在機制不要求靜態或動態類型檢查
儘管Java不支持潛在類型機制,可是這並不意味着有界泛型代碼不能再不一樣的類型層次結構之間應用。
經過反射實現潛在類型機制:
//: generics/LatentReflection.java // Using Reflection to produce latent typing. import java.lang.reflect.*; import static net.mindview.util.Print.*; // Does not implement Performs: class Mime { public void walkAgainstTheWind() {} public void sit() { print("Pretending to sit"); } public void pushInvisibleWalls() {} public String toString() { return "Mime"; } } // Does not implement Performs: class SmartDog { public void speak() { print("Woof!"); } public void sit() { print("Sitting"); } public void reproduce() {} } class CommunicateReflectively { public static void perform(Object speaker) { Class<?> spkr = speaker.getClass(); try { try { Method speak = spkr.getMethod("speak"); speak.invoke(speaker); } catch(NoSuchMethodException e) { print(speaker + " cannot speak"); } try { Method sit = spkr.getMethod("sit"); sit.invoke(speaker); } catch(NoSuchMethodException e) { print(speaker + " cannot sit"); } } catch(Exception e) { throw new RuntimeException(speaker.toString(), e); } } } public class LatentReflection { public static void main(String[] args) { CommunicateReflectively.perform(new SmartDog()); CommunicateReflectively.perform(new Robot()); CommunicateReflectively.perform(new Mime()); } } /* Output: Woof! Sitting Click! Clank! Mime cannot speak Pretending to sit *///:~
這些類徹底是彼此分離的,沒有任何公共基類或接口。經過反射動態肯定所須要的方法是否可用並調用它們。
使用可變參數來解決:
import java.lang.reflect.*; import java.util.*; import static net.mindview.util.Print.*; public class Apply { public static <T, S extends Iterable<? extends T>> void apply(S seq, Method f, Object... args) { try { for(T t: seq) f.invoke(t, args); } catch(Exception e) { // Failures are programmer errors throw new RuntimeException(e); } } } class Shape { public void rotate() { print(this + " rotate"); } public void resize(int newSize) { print(this + " resize " + newSize); } } class Square extends Shape {} class FilledList<T> extends ArrayList<T> { public FilledList(Class<? extends T> type, int size) { try { for(int i = 0; i < size; i++) // Assumes default constructor: add(type.newInstance()); } catch(Exception e) { throw new RuntimeException(e); } } } class ApplyTest { public static void main(String[] args) throws Exception { List<Shape> shapes = new ArrayList<Shape>(); for(int i = 0; i < 10; i++) shapes.add(new Shape()); Apply.apply(shapes, Shape.class.getMethod("rotate")); Apply.apply(shapes, Shape.class.getMethod("resize", int.class), 5); List<Square> squares = new ArrayList<Square>(); for(int i = 0; i < 10; i++) squares.add(new Square()); Apply.apply(squares, Shape.class.getMethod("rotate")); Apply.apply(squares, Shape.class.getMethod("resize", int.class), 5); Apply.apply(new FilledList<Shape>(Shape.class, 10), Shape.class.getMethod("rotate")); Apply.apply(new FilledList<Shape>(Square.class, 10), Shape.class.getMethod("rotate")); SimpleQueue<Shape> shapeQ = new SimpleQueue<Shape>(); for(int i = 0; i < 5; i++) { shapeQ.add(new Shape()); shapeQ.add(new Square()); } Apply.apply(shapeQ, Shape.class.getMethod("rotate")); } } /* (Execute to see output) *///:~
apple(0方法可接受任何實現了Iterable接口的事物,包括List這樣的全部Collection類。
好比下面這個類:
import java.util.*; public class SimpleQueue<T> implements Iterable<T> { private LinkedList<T> storage = new LinkedList<T>(); public void add(T t) { storage.offer(t); } public T get() { return storage.poll(); } public Iterator<T> iterator() { return storage.iterator(); } } ///:~
由於Iterable接口是已經內建的,而它正是咱們須要的,若是不存在恰好適合的接口呢?
import java.util.*; // Doesn't work with "anything that has an add()." There is // no "Addable" interface so we are narrowed to using a // Collection. We cannot generalize using generics in // this case. public class Fill { public static <T> void fill(Collection<T> collection, Class<? extends T> classToken, int size) { for(int i = 0; i < size; i++) // Assumes default constructor: try { collection.add(classToken.newInstance()); } catch(Exception e) { throw new RuntimeException(e); } } } class Contract { private static long counter = 0; private final long id = counter++; public String toString() { return getClass().getName() + " " + id; } } class TitleTransfer extends Contract {} class FillTest { public static void main(String[] args) { List<Contract> contracts = new ArrayList<Contract>(); Fill.fill(contracts, Contract.class, 3); Fill.fill(contracts, TitleTransfer.class, 2); for(Contract c: contracts) System.out.println(c); SimpleQueue<Contract> contractQueue = new SimpleQueue<Contract>(); // Won't work. fill() is not generic enough: // Fill.fill(contractQueue, Contract.class, 3); } } /* Output: Contract 0 Contract 1 Contract 2 TitleTransfer 3 TitleTransfer 4 *///:~
潛在類型機制意味着你能夠編寫代碼聲明:我不關心我再這裏使用的類型,只要它具備這些方法便可。實際上,潛在類型機制建立了一個包含所須要方法的隱試接口。所以它遵循這樣的規則:若是咱們手工編寫了必須的接口,那麼它就應該可以解決問題。
咱們可使用設配器來適配已有的接口,產生想要的接口:
import coffee.*; import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; interface Addable<T> { void add(T t); } public class Fill2 { // Classtoken version: public static <T> void fill(Addable<T> addable, Class<? extends T> classToken, int size) { for(int i = 0; i < size; i++) try { addable.add(classToken.newInstance()); } catch(Exception e) { throw new RuntimeException(e); } } // Generator version: public static <T> void fill(Addable<T> addable, Generator<T> generator, int size) { for(int i = 0; i < size; i++) addable.add(generator.next()); } } // To adapt a base type, you must use composition. // Make any Collection Addable using composition: class AddableCollectionAdapter<T> implements Addable<T> { private Collection<T> c; public AddableCollectionAdapter(Collection<T> c) { this.c = c; } public void add(T item) { c.add(item); } } // A Helper to capture the type automatically: class Adapter { public static <T> Addable<T> collectionAdapter(Collection<T> c) { return new AddableCollectionAdapter<T>(c); } } // To adapt a specific type, you can use inheritance. // Make a SimpleQueue Addable using inheritance: class AddableSimpleQueue<T> extends SimpleQueue<T> implements Addable<T> { public void add(T item) { super.add(item); } } class Fill2Test { public static void main(String[] args) { // Adapt a Collection: List<Coffee> carrier = new ArrayList<Coffee>(); Fill2.fill( new AddableCollectionAdapter<Coffee>(carrier), Coffee.class, 3); // Helper method captures the type: Fill2.fill(Adapter.collectionAdapter(carrier), Latte.class, 2); for(Coffee c: carrier) print(c); print("----------------------"); // Use an adapted class: AddableSimpleQueue<Coffee> coffeeQueue = new AddableSimpleQueue<Coffee>(); Fill2.fill(coffeeQueue, Mocha.class, 4); Fill2.fill(coffeeQueue, Latte.class, 1); for(Coffee c: coffeeQueue) print(c); } } /* Output: Coffee 0 Coffee 1 Coffee 2 Latte 3 Latte 4 ---------------------- Mocha 5 Mocha 6 Mocha 7 Mocha 8 Latte 9 *///:~
策略設計模式,這種設計模式能夠產生更優雅代碼,由於它將變化的事物徹底隔離到了一個函數對象中。
函數對象的價值就在於,於普通方法不一樣,它們能夠傳遞出去,而且還能夠擁有在多個調用之間持久化的狀態。
函數對象主要是由其目的來區別的。這裏的目的就是要建立某種事物,使它的行爲就像是一個能夠傳遞出去的單個方法同樣。
//: generics/Functional.java import java.math.*; import java.util.concurrent.atomic.*; import java.util.*; import static net.mindview.util.Print.*; // Different types of function objects: interface Combiner<T> { T combine(T x, T y); } interface UnaryFunction<R,T> { R function(T x); } interface Collector<T> extends UnaryFunction<T,T> { T result(); // Extract result of collecting parameter } interface UnaryPredicate<T> { boolean test(T x); } public class Functional { // Calls the Combiner object on each element to combine // it with a running result, which is finally returned: public static <T> T reduce(Iterable<T> seq, Combiner<T> combiner) { Iterator<T> it = seq.iterator(); if(it.hasNext()) { T result = it.next(); while(it.hasNext()) result = combiner.combine(result, it.next()); return result; } // If seq is the empty list: return null; // Or throw exception } // Take a function object and call it on each object in // the list, ignoring the return value. The function // object may act as a collecting parameter, so it is // returned at the end. public static <T> Collector<T> forEach(Iterable<T> seq, Collector<T> func) { for(T t : seq) func.function(t); return func; } // Creates a list of results by calling a // function object for each object in the list: public static <R,T> List<R> transform(Iterable<T> seq, UnaryFunction<R,T> func) { List<R> result = new ArrayList<R>(); for(T t : seq) result.add(func.function(t)); return result; } // Applies a unary predicate to each item in a sequence, // and returns a list of items that produced "true": public static <T> List<T> filter(Iterable<T> seq, UnaryPredicate<T> pred) { List<T> result = new ArrayList<T>(); for(T t : seq) if(pred.test(t)) result.add(t); return result; } // To use the above generic methods, we need to create // function objects to adapt to our particular needs: static class IntegerAdder implements Combiner<Integer> { public Integer combine(Integer x, Integer y) { return x + y; } } static class IntegerSubtracter implements Combiner<Integer> { public Integer combine(Integer x, Integer y) { return x - y; } } static class BigDecimalAdder implements Combiner<BigDecimal> { public BigDecimal combine(BigDecimal x, BigDecimal y) { return x.add(y); } } static class BigIntegerAdder implements Combiner<BigInteger> { public BigInteger combine(BigInteger x, BigInteger y) { return x.add(y); } } static class AtomicLongAdder implements Combiner<AtomicLong> { public AtomicLong combine(AtomicLong x, AtomicLong y) { // Not clear whether this is meaningful: return new AtomicLong(x.addAndGet(y.get())); } } // We can even make a UnaryFunction with an "ulp" // (Units in the last place): static class BigDecimalUlp implements UnaryFunction<BigDecimal,BigDecimal> { public BigDecimal function(BigDecimal x) { return x.ulp(); } } static class GreaterThan<T extends Comparable<T>> implements UnaryPredicate<T> { private T bound; public GreaterThan(T bound) { this.bound = bound; } public boolean test(T x) { return x.compareTo(bound) > 0; } } static class MultiplyingIntegerCollector implements Collector<Integer> { private Integer val = 1; public Integer function(Integer x) { val *= x; return val; } public Integer result() { return val; } } public static void main(String[] args) { // Generics, varargs & boxing working together: List<Integer> li = Arrays.asList(1, 2, 3, 4, 5, 6, 7); Integer result = reduce(li, new IntegerAdder()); print(result); result = reduce(li, new IntegerSubtracter()); print(result); print(filter(li, new GreaterThan<Integer>(4))); print(forEach(li, new MultiplyingIntegerCollector()).result()); print(forEach(filter(li, new GreaterThan<Integer>(4)), new MultiplyingIntegerCollector()).result()); MathContext mc = new MathContext(7); List<BigDecimal> lbd = Arrays.asList( new BigDecimal(1.1, mc), new BigDecimal(2.2, mc), new BigDecimal(3.3, mc), new BigDecimal(4.4, mc)); BigDecimal rbd = reduce(lbd, new BigDecimalAdder()); print(rbd); print(filter(lbd, new GreaterThan<BigDecimal>(new BigDecimal(3)))); // Use the prime-generation facility of BigInteger: List<BigInteger> lbi = new ArrayList<BigInteger>(); BigInteger bi = BigInteger.valueOf(11); for(int i = 0; i < 11; i++) { lbi.add(bi); bi = bi.nextProbablePrime(); } print(lbi); BigInteger rbi = reduce(lbi, new BigIntegerAdder()); print(rbi); // The sum of this list of primes is also prime: print(rbi.isProbablePrime(5)); List<AtomicLong> lal = Arrays.asList( new AtomicLong(11), new AtomicLong(47), new AtomicLong(74), new AtomicLong(133)); AtomicLong ral = reduce(lal, new AtomicLongAdder()); print(ral); print(transform(lbd,new BigDecimalUlp())); } } /* Output: 28 -26 [5, 6, 7] 5040 210 11.000000 [3.300000, 4.400000] [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] 311 true 265 [0.000001, 0.000001, 0.000001, 0.000001] *///:~