Generics is the most important feature of JDK 1.5, mainly to process Collection.
The following code is debugged in JDK 1.5.
Code Example 1: Demo.java
package maoxiang.examples.jdk15.generics; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map ; / *** @Author Mao * * Demonstrate how to use the generics feature. The code comes from the Generics tutorial: * http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf* * generics is similar to the template in C . * Difference: * 1. * 2. * / Public class demo {public static void main (String [] args) {} / ** * The simplest usage * / public void test1 () {// previous usage /// List myintlist = new linkedList (); // 1 //myintlist.add(new integer (0)); // 2 // Integer x = (Integer) MyintList.ITerator (). Next (); // 3 requires forced Convert // 1.5 Usage List Myintlist = new linkedlist (); // 1 'Myintlist.add (new integer (0)); // 2' Integer x = MyintList.iterator (). Next. ); // 3 '} / ** * Usage of anonymous characters * / public void test2 () {list list = new arraylist (); // Print a collection Wildcards (List) via anonymous characters; Wildcards1 (); / * * If WildCards2 is defined as Wildcards2 (List Shapes) * The following call error * / wildcards2 (list);} public void Wildcards (Collection > C) {// previous usage // itemator i = c.iterator (); // for (int K = 0; k c Represents for (Object E: c) {log (e);}} public void wildcards1 () {// collection > c = new arraylist (); //c.add(new Object ()); // compile time error // The above is the wrong usage, because Determine the type C, you cannot use add, but get it can.
The correct usage is as follows: arraylist c = new arraylist (); C.Add ("test"); list > List = C; log (c.get (0));} public void Wildcards2 (List Extendeds Shape> Shapes) {// list Shapes Definition can only accept list shapes, nor can you accept list for (Shape S: Shapes) {s.draw (); / The following method is wrong, because the parameters are declared as Extends Shpape, which cannot determine that Rectangle is Shape subclass, which belongs to unsecurity calls //shapes.add (0, new rectangle ()); Map alldrivers = new hashmap < String, driver> (); census.addregistry; // The following is allowed, because Drivers are clearly defined, List drivers = new arraylist (); census.add (drivers);} / ** * General Methods Usage ** / public void test3 () {// Applicable to various types of functions Object [] OA = new object [100]; Collection
PARAM Shapes * / Public Void Drawall (list extends Shape> Shapes) {list > History = new arraylist > (); History.Add (Shapes); for Shape s: shapes) {s.draw ();}} / *** ** / public void test4 () {list l1 = new arraylist (); list l2 = new arraylist < Integer> (); system.out.print (l1.getclass () == l2.getClass ()); // Print to true,} / *** Error usage * / public void test5 () {Collection CS = New ArrayList (); // The following is the method of error // if (cs instanceof color ) {} // illegal // below is a warning usage // collection cstr = (Collection ) CS ; // unchecked // Warning} public void test6 () {// error usage // list [] lsa = new list [10]; // not real allowedlist > [] lsa = new List > [10]; // ok, array of unbounded wildcard // typeObject o = lsa; object [] OA = (Object []) O; list Li = new arraylist (); li .add (New Integer (3)); OA [1] = li; // Correct // String S = LSA [1] .get (0); // Run-Time Error - ClassCastexception // String S = LSA [ 1]. GET (0); // Run Time Error, But We Wre WarnedString S = (String) LSA [1] .Get (0); // Run Time Error, But Cast IS // Explicit} Public Void Test7 () {SINK s = null; SINK S1 = NULL; Collection CS = NULL; STRING STR = WriteAll (CS, S1); // String Str = WriteAll (CS, S); // Invalid call Object Obj = WriteAll1 (CS, S); // Correct call Str = WriteAll2 (CS, S1); // Correct call} public T Writeall (Collection Coll, Sink snk) {t Last = NULL; for (t t: coll) {Last = T; snk.flush (last);
} Return Last;} public T WriteAll1 (Collection SNK) {t Last = NULL; for (t t: COLL) {Last = T; snk.flush (last) } Return Last;} public t writeall2 (Collection COLL, SINK Super T> SNK) {t Last = NULL; For (t t: coll) {Last = T; snk.flush (Last );}}} // Print Private Void Log (Object OB) {system.out.print (ob);} // Auxiliary Definition Abstract Class Shape {public Abstract Void Draw ();} Class Circle Extends Shape { private int x, y, radius; public void draw () {}} class Rectangle extends Shape {private int x, y, width, height; public void draw () {}} class Person {} class Driver extends Person {} class Census {public static void addRegistry (Map registry) {} public static void add (List extends Person> persons) {}} class Collections {public static void copy ( List DEST, LIST SRC) {} code Example 2. Sink.java
Package maoxiang.examples.jdk15.generics; / *** * @Author Mouxiang * * Defines an interface template to simplify the definition of the interface * * / Interface Sink {public void flush (e t);} / * * If it is the previous definition, you want to define various types of interfaces, obviously more troublesome * Interface Sink {* * public void flush (string str); * public void flush; * public void flush ); * ... *} * /