1. Introduce
2. Define simple Java Generics
The Java generics are actually creating a class that uses a type as a parameter. As we write the methods of the class, the method is such as method (String str1,string str2), and the values of the parameters str1 and str2 in the method are variable. And generics are the same, so write class Java_generics<k,v>, where the edges of K and V are like parameters str1 and str2 in the method, and also variable. Here's a look at the example:
Import java.util.Hashtable;
Class testgen0<k,v>{
Public hashtable<k,v> h=new hashtable<k,v> ();
public void put (k K, v V) {
H.put (K,V);
}
Public V get (k k) {
Return H.get (k);
}
public static void Main (String args[]) {
Testgen0<string,string> t=new testgen0<string,string> ();
T.put ("Key", "value");
String s=t.get ("key");
System.out.println (s);
}
}
Correct output: value
This is just an example, but see if you create a class that uses a type as a parameter, the argument is k,v, and the incoming value is a string type. This class does not have a specific type to treat, previously we have defined a class, in the input parameters are fixed, what type is required, but now write the program, you can not set the type of parameters, the specific use of the time to determine, increased the generality of the program, such as a template.
3. Generic wildcard character
First, here is an example of how to print out all the elements in a set, we first use the old version of the jdk1.4 coding rules, the code is as follows:
void Printcolleciton (Collection c) {
Iterator i = C.iterator ();
for (k = 0; k < c.size (); k++) {
System.out.pritnln (I.next ();
}
Then, we use the jdk5.0 generics to rewrite the above code (the syntax of the loop is the new version of the syntax):
void Printcollection (colleciton<object> c) {
for (Object e:c) {
System.out.print (e);
}
}
This new version is not much better than the old version, and the old version can be invoked as a parameter with any of the collection types, while the newer versions only hold collection<object> types,colleciton<object> is not a superclass of any type of collection.
So what is a super type for all Colleciton types? It is collection<?> such a type, read as "Unknown Colleciton". It means that the Colleciton element type can match any type, which we call the wildcard type, which we write:
void Printcollection (colleciton<?> c) {
for (Object e:c) {
System.out.println (e);
}
}
Now we call it with any type of collection, and we need to be aware that the internal method Printcolleciton (), we can read the elements from C, and these elements are type object and are safe because, regardless of the type in the collection, it always includes object, However, it is not safe to add any object to the collection:
colleciton<?> C = new arraylist<string> ();
C.add (New Object ());//compile-time error
Since we do not know what type of element C holds, we cannot add object to the set. The Add () method uses type E as an argument, (the element type of the collection) when the true argument type is? , it represents a number of unknown types. Any argument passed to the Add () method must be a subtype of this unknown type. Because we do not know the unknown type, so we pass it to everything. The main exception is NULL, which is a member of each type.
On the other hand, suppose to give a list<?>, we call get () and make the most of the result. The result type is an unknown type. But I always knew it was an object, so assigning a result from Get () to an object variable was safe, or passed as a parameter to a place that needed an object type.