Special note: Although Sun named the new version of JDK by Chinese habit 5.0, although the new version of the new version is so much, I still have to call new versions as JDK 1.5.
Generics is the most important feature of JDK 1.5, mainly to process Collection.
Detailed generics tutorial: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf.
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 Xiang
*
* 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 Templates in C .
* the 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 conversion
// 1.5 usage
List
Myintlist.add (new integer (0)); // 2 '
Integer x = MyintList.iterator (). Next (); // 3 '
}
/ **
* Usage of anonymous characters
* /
Public void test2 () {
List
// Print a collection by anonymous character
Wildcards (List);
Wildcards1 ();
/ *
* If Wildcards2 is defined as Wildcards2 (List
* The following call error
* /
Wildcards2 (List);
}
Public void Wildcards (Collection > c) {
// previous usage
// Iterator i = C.Itemrator ();
// for (int K = 0; k // Log (i.next ()); //} //1.5 usage // Collection > C For (Object E: C) { Log (e); } } Public void wildcards1 () { // Collection > C = new arraylist //c.add (new object ()); // compile time error // The above is a wrong usage, because the type of C cannot be determined, the add, but Get can. The correct usage is as follows: arraylist C.Add ("test"); List > List = C; LOG (C.Get (0)); } Public Void Wildcards2 (List extends Shape> Shapes) { // list For (Shape S: Shapes) { s.draw (); } // The following means is wrong, because the parameters are declared as Extends Shpape, and the Rectangle is not a SHAPE subclass, which belongs to unsecurity calls. //shapes.add(0, new rectangle ()); Map Census.addRegistry (alldrivers); // The following is allowed, because Drivers are clearly defined. List Census.add (drivers); } / ** * Generic Methods usage * * / Public void test3 () { / / Suitable for various types of functions Object [] OA = New Object [100]; Collection FromArrayTocollection (OA, CO); // Tinferred to Be Object String [] sa = new string [100]; Collection FromArrayTocollection (SA, CS); // Tinferred to Be String FromArrayTocollection (SA, CO); // Tinferred to Be Object Integer [] IA = new integer [100]; Float [] fa = new float [100]; Number [] na = new number [100]; Collection FromArrayTocollection (IA, CN); // Tinferred to Be Number FromArrayTocollection (Fa, CN); // Tinferred to Be Number FromArrayTocollection (NA, CN); // Tinferred to Be Number FromArrayTocollection (NA, CO); // Tinferred to Be Object //test.fromaaraytocollection (na, cs); // error usage } Public For (t o: a) { / / If the parameter is defined as a collection > C below to write error C.Add (o); // Compile Time Error } } / ** * generics nested usage * @Param Shapes * / Public void Drawall (List extends Shape> Shapes) { List History.add (shapes); For (Shape S: Shapes) { s.draw (); } } / ** * * * / Public void test4 () { List List System.out.print (l1.getclass () == l2.getClass ()); // Print to True, } / ** * Error usage * / Public void test5 () { Collection CS = New ArrayList / / The following is the wrong use // IF (cs instanceof color // The following is a warning usage // Collection // Warning } Public void test6 () { // Error usage // list List > [] Lsa = new list > [10]; // ok, array of unbounded wildcard // Type Object o = lsa; Object [] OA = (Object []) O; List 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 worth Warned String s = (string) LSA [1] .get (0); // Run Time Error, But Cast IS // Explicit } Public void test7 () { SINK SINK Collection 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 last = null; For (t t: coll) { Last = T; SNK.FLUSH (LAST); } Return Last; } PUBLIC T last = null; For (t t: coll) { Last = T; SNK.FLUSH (LAST); } Return Last; } Public T last = null; For (t t: coll) { Last = T; SNK.FLUSH (LAST); } Return 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 } Public Static Void Add (List extends Person> Persons) { } } Class collections { Public Static } } Code Example 2. Sink.java Package maoxiang.examples.jdk15.generics; / ** * * @Author Mao Xiang * * Define an interface template to simplify the definition of the interface * * / Interface Sink Public void flush (e t); } / * * If it is the previous definition, it is clear that the interface to be various types is clear. * Interface Sink { * * Public Void Flush (String STR); * Public void flush (Object obj); * Public Void Flush (Integer Test); * ... *} * /> History = new arraylist
> ();
SRC) {