Java generics (Generics) are a new feature introduced in JDK5 that allow the use of type parameters (Type Parameter) when defining classes and interfaces. The declared type parameters are replaced with specific types when used. The most important application of generics now is in the new collection class framework in JDK5, where Map and List are used. The advantages are self-evident. We can extend more classes horizontally. The disadvantages are actually its advantages, because when we use generic classes, we must be very clear about the purpose of our code and cannot use errors. type.
The most basic generic class
Copy the code code as follows:
package com.garinzhang.javabase.generic.e1;
/**
* The most basic generic class, the type is defined by yourself
* @author Garin Zhang
*
* @param <T>
*/
public class Point<T> {
private T var;
public T getVar() {
return var;
}
public void setVar(T var) {
this.var = var;
}
}
package com.garinzhang.javabase.generic.e1;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Point<String> p = new Point<String> ();
p.setVar("coder");
System.out.println(p.getVar());
}
}
Multiple generic types
Copy the code code as follows:
package com.garinzhang.javabase.generic.e2;
/**
* Multiple generic types. Generally, it is best to use letters close to T, such as S, R, etc.
* @author Garin Zhang
*
* @param <T>
* @param <S>
*/
public class Notepad<T, S> {
private T key;
private S value;
public T getKey() {
return this.key;
}
public S getValue() {
return this.value;
}
public void setKey(T key) {
this.key = key;
}
public void setValue(S value) {
this.value = value;
}
}
package com.garinzhang.javabase.generic.e2;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Notepad<String, Integer> p = new Notepad<String, Integer> ();
p.setKey("coder");
p.setValue(99999);
System.out.println("key: " + p.getKey());
System.out.println("value: " + p.getValue());
}
}
Use wildcard "?" in method parameters
Copy the code code as follows:
package com.garinzhang.javabase.generic.e3;
/**
* The key to this example is in the main method
* @author Garin Zhang
*
* @param <T>
*/
public class Info<T> {
private T key;
public T getKey() {
return this.key;
}
public void setKey(T key) {
this.key = key;
}
@Override
public String toString() {
return this.key.toString();
}
}
package com.garinzhang.javabase.generic.e3;
/**
* Use wildcards in method parameters
* @author Garin Zhang
*
*/
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info<String> i = new Info<String>();
i.setKey("coder");
fun(i);
Info<Integer> j = new Info<Integer>();
j.setKey(9999);
fun(j);
}
public static void fun(Info<?> temp) {
System.out.println("Content: " + temp);
}
}
Upward transformation fails
Copy the code code as follows:
package com.garinzhang.javabase.generic.e4;
/**
* The key to this example is in the main method
* @author Garin Zhang
*
* @param <T>
*/
public class Info<T> {
private T key;
public T getKey() {
return this.key;
}
public void setKey(T key) {
this.key = key;
}
@Override
public String toString() {
return this.key.toString();
}
}
package com.garinzhang.javabase.generic.e4;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info<String> strEg = new Info<String>();
Info<Object> objEg;
// Compilation error "Type mismatch: cannot convert from Info<String> to Info<Object>"
//Upcast failed, String -> Object
// objEg = strEg;
}
}
The use of generics in interfaces
Copy the code code as follows:
package com.garinzhang.javabase.generic.e5;
/**
* The key to this example is in the main method
* @author Garin Zhang
*
* @param <T>
*/
interface Info<T> {
public T getVar();
}
package com.garinzhang.javabase.generic.e5;
/**
* Generic class
* @author Garin Zhang
*
* @param <T>
*/
public class InfoImpl<T> implements Info<T> {
private T var;
public InfoImpl(T var) {
this.setVar(var);
}
public void setVar(T var) {
this.var = var;
}
public T getVar() {
return this.var;
}
}
package com.garinzhang.javabase.generic.e5;
/**
* Non-generic class
* @author Garin Zhang
*
* @param <T>
*/
public class InfoImpl1 implements Info<String> {
private String var;
public InfoImpl1(String var) {
this.setVar(var);
}
public void setVar(String var) {
this.var = var;
}
public String getVar() {
return this.var;
}
}
package com.garinzhang.javabase.generic.e5;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info<String> strEg = new InfoImpl<String>("coder");
System.out.println("Content: " + strEg.getVar());
Info<String> strEg1 = new InfoImpl1("coder1");
System.out.println("Content: " + strEg1.getVar());
}
}
Wildcards and the use of extends and super
Copy the code code as follows:
package com.garinzhang.javabase.generic.e6;
/**
* The key to this example is in the main method
* @author Garin Zhang
*
* @param <T>
*/
public class Info<T> {
private T key;
public T getKey() {
return this.key;
}
public void setKey(T key) {
this.key = key;
}
@Override
public String toString() {
return this.key.toString();
}
}
package com.garinzhang.javabase.generic.e6;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info<String> strEg = new Info<String>();
strEg.setKey("coder");
//Compilation error "The method fun(Info<? extends Number>) in the type GenericExample is not applicable for the arguments (Info<String>)"
// upTypeLimit(i);
// Use Integer or Number types.
Info<Integer> intEg = new Info<Integer>();
intEg.setKey(9999);
upTypeLimit(intEg);
//Compilation error "The method downTypeLimit(Info<? super String>) in the type GenericExample is not applicable for the arguments (Info<Integer>)"
// downTypeLimit(intEg);
// Since super is used, downTypeLimit can only receive String itself and Object
// Checked the inheritance relationship of String, no other classes are inherited, only Object
downTypeLimit(strEg);
Info<Object> objEg = new Info<Object>();
objEg.setKey(999);
downTypeLimit(objEg);
}
/**
* <? extends T> represents the upper bound of the type, indicating that the parameterized type may be T or a subclass of T
* @param temp
*/
public static void upTypeLimit(Info<? extends Number> temp) {
System.out.println("Content: " + temp);
}
/**
* <? super T> represents the type lower bound (called super type qualification in Java Core), indicating that the parameterized type is the super type (parent type) of this type, until Object
* In this example, it means that T can only be Object or String, because String only inherits from Object
* @param temp
*/
public static void downTypeLimit(Info<? super String> temp) {
System.out.println("Content: " + temp);
}
}
Method generics, multiple generics in the method
Copy the code code as follows:
package com.garinzhang.javabase.generic.e7;
/**
* Method generics, multiple generics in the method
* @author Garin Zhang
*
* @param <T>
*/
public class Info {
/**
* Format: method modification <comma-separated type list> return value type method name (parameter list)
* For example: public <T, S> T fun(T t, S s)
* @param t
* @param s
* @return
*/
public <T, S> T fun(T t, S s) {
System.out.println(s.toString());
return t;
}
}
package com.garinzhang.javabase.generic.e7;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info info = new Info();
String str = info.fun("coder", "print second generic param");
System.out.println(str);
int i = info.fun(30, "print second param again");
System.out.println(i);
}
}
The generic type passed in or returned in the method is determined by the parameter type set when the method is called.
Copy the code code as follows:
package com.garinzhang.javabase.generic.e8;
/**
* extends
* @author Garin Zhang
*
* @param <T>
*/
public class Info<T extends Number> {
private T var;
public T getVar() {
return this.var;
}
public void setVar(T var) {
this.var = var;
}
@Override
public String toString() {
return this.var.toString();
}
}
package com.garinzhang.javabase.generic.e8;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info<Integer> intEg = fun(30); // The type here has been determined to be Integer
System.out.println(intEg.getVar());
}
/**
* The generic type passed in or returned in the method is determined by the parameter type set when calling the method.
* @param param
* @return
*/
public static <T extends Number> Info<T> fun(T param) {
Info<T> temp = new Info<T>();
temp.setVar(param);
return temp;
}
}
Make the two parameter types passed in the method consistent
Copy the code code as follows:
package com.garinzhang.javabase.generic.e9;
/**
* View main
* @author Garin Zhang
*
* @param <T>
*/
public class Info<T> {
private T var;
public T getVar() {
return this.var;
}
public void setVar(T var) {
this.var = var;
}
@Override
public String toString() {
return this.var.toString();
}
}
package com.garinzhang.javabase.generic.e9;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info<String> i1 = new Info<String>();
i1.setVar("Hello");
Info<String> i2 = new Info<String>();
i2.setVar("Coder");
Info<Integer> i3 = new Info<Integer>();
i3.setVar(999);
add(i1, i2);
//Compilation error "The method add(Info<T>, Info<T>) in the type GenericExample is not applicable for the arguments (Info<String>, Info<Integer>)"
// add(i1, i3);
}
/**
* The two parameter types passed in the method must be consistent
* @param param
* @return
*/
public static <T> void add(Info<T> i1, Info<T> i2) {
System.out.println(i1.getVar() + ":" + i2.getVar());
}
}
Generics, variable parameters, similar to the Arguments object in JavaScript
Copy the code code as follows:
package com.garinzhang.javabase.generic.e10;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Integer i[] = fun(1, 2, 3, 4, 5, 6);
fun2(i);
}
public static <T> T[] fun(T... arg) {
return arg;
}
public static <T> void fun2(T param[]) {
System.out.println("generic array: ");
for(T t : param) {
System.out.println(t + " ,");
}
}
}
Generic nesting: use generic classes as parameters; determine the return value based on the return value type
Copy the code code as follows:
package com.garinzhang.javabase.generic.e11;
/**
* Accepts two generic types
* @author Garin Zhang
*
* @param <T>
*/
public class Info<T, V> {
private T var;
private V value;
public T getVar() {
return this.var;
}
public void setVar(T var) {
this.var = var;
}
public V getValue(){
return this.value;
}
public void setValue(V value) {
this.value = value;
}
@Override
public String toString() {
return this.var.toString();
}
}
package com.garinzhang.javabase.generic.e11;
/**
* Accepts 1 generic type
* @author Garin Zhang
*
* @param <T>
*/
public class Demo<S> {
private S info;
public Demo(S info) {
this.setInfo(info);
}
public void setInfo(S info) {
this.info = info;
}
public S getInfo() {
return this.info;
}
}
package com.garinzhang.javabase.generic.e11;
import java.util.List;
import com.google.common.collect.Lists;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Demo<Info<String, Integer>> d;
Info<String, Integer> i;
i = new Info<String, Integer>();
i.setVar("Coder");
i.setValue(999);
d = new Demo<Info<String,Integer>>(i);
System.out.println("Content: " + d.getInfo().getVar());
System.out.println("Content: " + d.getInfo().getValue());
System.out.println(query(1, 2, 3, 4, 5).toString()); // [1, 2, 3, 4, 5]
// Warning "Type safety: A generic array of Object&Comparable<?>&Serializable is created for a varargs parameter"
System.out.println(query(1, 2, 3, "StringType").toString()); // [1, 2, 3, StringType]
System.out.println(query("I ", "am ", "a ", "coder").toString());// [I , am , a , coder]
List<String> list = Lists.newArrayList("I ", "am ", "a ", "coder");
System.out.println(list.toString()); // [I , am , a , coder]
}
/**
* Determine the generic type through the return value. The return value type in this method is automatically generated by the definition of the method.
* @param elements
* @return
*/
public static <E> List<E> query(E... elements) {
// https://github.com/exitsoft/exit-web-framework/commit/1d2f1098a2a4b6abab175b793e2308aa8bd0ea16.
//import com.google.common.collect.Lists;
// <dependency>
// <groupId>com.google.guava</groupId>
// <artifactId>guava</artifactId>
// <version>16.0.1</version>
// </dependency>
return Lists.newArrayList(elements);
}
}