Generics is one of the most important features of JDK 1.5, and is primarily used to handle collection.
The following code is passed in JDK 1.5 debugging.
Code Instance 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 Maoxiang
*
* Demonstrates how to use the generics feature. Code from the Generics tutorial:
* Http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
*
* Generics is similar to a template in C + +.
* Difference:
* 1.
* 2.
*/
public class Demo {
public static void Main (string[] args) {}
/**
* The simplest use of
*/
public void Test1 () {
//Previous usage
List myintlist = new LinkedList (); 1
//myintlist.add (New Integer (0)); 2
//integer x = (Integer) myintlist.iterator (). Next (); 3 Need to cast
//1.5 usage
list<integer> myintlist = new linkedlist<integer> (); 1 '
Myintlist.add (New Integer (0)); 2 '
Integer x = Myintlist.iterator (). Next (); 3 '
}
/**
* Use of anonymous characters
*/
public void Test2 () {
list<circle> List = new arraylist<circle> ();
//Print a collection with anonymous characters
wildcards (list);
Wildcards1 ();
/*
* If WILDCARDS2 is defined as WILDCARDS2 (List <Shape> shapes)
* The following call error
*/
Wildcards2 (list);
}
public void wildcards (collection< > C) {
//Previous usage
//iterator i = C.iterator ();
//for (int k = 0; k < c.size (); k++) {
//
log (I.next ());
//}
The use of
//1.5
//collection<?> C means
for (Object e:c) {
log (e);
}
}
public void Wildcards1 () {
//collection<?> C = new arraylist<string> ();
//c.add (New Object ()); Compile time error
//Above is the wrong usage, because you cannot determine the type of C, you cannot use Add, but get can. The correct usage is as follows:
arraylist<string> C = new arraylist<string> ();
c.add ("test");
list<? > list = c;
log (c.get (0));
}
public void Wildcards2 (list<? extends shape> shapes) {
list<shape> shapes definition can only accept list<shape> shapes, also can not accept list<circle>
for (Shape s:shapes) {
S.draw ();
}
//The following spelling error, because the parameter is declared as extends Shpape, cannot be determined rectangle as a shape subclass, is an unsafe call
//shapes.add (0, New Rectangle ());
map<string, driver> alldrivers = new hashmap<string, driver> ();
census.addregistry (alldrivers);
//The following wording is allowed, as drivers clearly defined,
list<driver> drivers = new arraylist<driver> ();
Census.add (drivers);
}
/**
Use of
* Generic Methods
*
*/
public void Test3 () {
//applicable to various types of functions
object[] oa = new object[100];
collection<object> CO = new arraylist<object> ();
Fromarraytocollection (OA, CO);/T inferred to being Object
string[] sa = new string[100];
collection<string> cs = new arraylist<string> ();
fromarraytocollection (SA, CS);//T inferred to is String
fromarraytocollection (SA, CO);/T inferred to is Object
integer[] ia = new integer[100];
float[] fa = new FLOAT[100];
number[] na = new number[100];
collection<number> cn = new Arraylist<number> ();
Fromarraytocollection (IA, CN);//T inferred to is number
fromarraytocollection (FA, CN);//T inferred to is number
fromarraytocollection (Na, CN);/T inferred to is number
fromarraytocollection (NA, CO);/T inferred to is Object
//test.fromarraytocollection (NA, CS);//Error usage
}
public <T> void Fromarraytocollection (t[] A, collection<t> c) {
for (T o:a) {
//If the parameter is defined as collection<? > c The following spelling error
C.add (o); Compile time error
}
}
/**
* Generics Embedding method
* @param shapes
*/
public void Drawall (list<? extends shape> shapes) {
list<list<? Extends shape>> history = new arraylist<list<? Extends Shape>> ();
history.add (shapes);
for (Shape s:shapes) {
S.draw ();
}
}
/**
*
*
*/
public void Test4 () {
list<string> l1 = new arraylist<string> ();
list<integer> L2 = new arraylist<integer> ();
System.out.print (l1.getclass () = = L2.getclass ());
//Print to True,
}
/**
* ERROR Usage
*/
public void Test5 () {
Collection cs = new arraylist<string> ();
//Below is the error usage
//if (cs instanceof collection<string>) {}//illegal
//Below is a warning usage
//collection<string> CStr = (collection<string>) CS; Unchecked
//Warning
}
public void Test6 () {
//Error usage
//list<string>[] LSA = new list<string>[10]; Not really allowed
list<? >[] LSA = new list<? >[10]; OK, array of unbounded wildcard
//Type
Object o = LSA;
object[] oa = (object[]) o;
list<integer> li = new arraylist<integer> ();
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 were warned
string s = (string) lsa[1].get (0); Run time error, but cast is
//Explicit
}
public void Test7 () {
sink<object> s = null;
sink<string> S1 = null;
collection<string> cs = null;
String str = writeall (cs, S1);
//string str = Writeall (cs, s); Invalid call
Object obj = writeAll1 (cs, s); Correct call to
str = WRITEALL2 (cs, S1); Correct call to
}
public <T> T Writeall (collection<t> Coll, sink<t> SNK) {
T last = null;
for (T t:coll) {
last = t;
Snk.flush (last);
}
return to last;
}
public <T> T writeAll1 (collection< extends t> Coll, sink<t> SNK) {
T last = null;
for (T t:coll) {
last = t;
Snk.flush (last);
}
return to last;
}
public <T> T writeAll2 (collection<t> Coll, sink<? Super T> SNK) {
T last = null;
for (T t:coll) {
last = t;
Snk.flush (last);
}
return to 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<string,? extends Person> registry) {
}
public static void Add (list< extends person> persons) {
}
}
class Collections {
public static <t, S extends t> void copy (list<t> dest, list<s> src) {
}
}