Multiple Java generic examples Shared

  • 2020-04-01 03:14:13
  • OfStack

1. A generic class

1.1 general generics


package test.lujianing;

class Test<T>{
    private T obj;
    public void setValue(T obj){
        this.obj =obj;
    }
    public T getValue(){
        System.out.println(obj.getClass().getName());
        return obj;
    }
}

public class TestOne {
    public static void main(String[] args) {
        //Test Integer generics
        Test<Integer> t1 = new Test<Integer>();
        t1.setValue(5);
        Integer i = t1.getValue();
        System.out.println(i);
        //Test for Double generics
        Test<Double> t2 = new Test<Double>();
        t2.setValue(5.55D);
        Double d = t2.getValue();
        System.out.println(d);
        //Test String generics
        Test<String> t3 = new Test<String>();
        t3.setValue("hello world");
        String str =t3.getValue();
        System.out.println(str);
    }
}

Output results:


java.lang.Integer
5
java.lang.Double
5.55
java.lang.String
hello world

1.2 K/V generics


package test.lujianing;
import java.util.HashMap;
import java.util.Map;

class TestKV<K,V>{
    private Map<K,V> map=new HashMap<K, V>();
    public void put(K k, V v) {
        map.put(k,v);
    }
    public V get(K k) {
        return map.get(k);
    }
}
public class TestFour{
    public static void main(String[] args) {
        TestKV<String,String> t = new TestKV<String, String>();
        t.put("name","jianing");
        System.out.println(t.get("name"));
        TestKV<String,Integer> t2 = new TestKV<String, Integer>();
        t2.put("age",24);
        System.out.println(t2.get("age"));
    }
}

Output results:


jianing
24

2. Generic interface


package test.lujianing;

public interface TestImpl<T> {
    public void setValue(T t);
    public T getValue();
}

Output results:


1
hello word

3. Generic methods


package test.lujianing;

class TestMethod{
    
    public <T>T getValue(Object s,Class<T> clazz) {
        System.out.println(clazz.getName());
        T t =null;
        if(clazz.getName().equals("java.lang.Integer")){
            Double d = Double.parseDouble(s.toString());
            int i =d.intValue();
            t=(T)new Integer(i);
        }
        if(clazz.getName().equals("java.lang.Double")){
            t=(T)new Double(s.toString());
        }
        return t;
    }
}

public class TestThree {
    public static void main(String[] args) {
        TestMethod t = new TestMethod();
        int i =t.getValue("30.0011",Integer.class);
        System.out.println(i);
        double d  =t.getValue("40.0022",Double.class);
        System.out.println(d);
    }
}

Output results:


java.lang.Integer
30
java.lang.Double
40.0022

Limit generics

In the above example, there is no restriction on class Test< T> The scope of the type holder T, the default qualified type equivalent to Object. Let's say we want to restrict T to a numeric interface type. Just do this: class Test< T extends Number> , so that the generic T in the class can only be the implementation class of the Number interface, passing in the non-number interface will cause errors.

5. Generics


package test.lujianing;
import java.util.HashMap;
import java.util.Map;

public class TestFive {
    public static void main(String[] args) {
        Map<String,Class<? extends Number>> map = new HashMap<String,Class<? extends Number>>();
        map.put("Integer",Integer.class);
        map.put("Double",Double.class);
        for (Map.Entry<String,Class<? extends Number>> entry : map.entrySet()) {
            System.out.println("key : " + entry.getKey() + " value : " + entry.getValue());
        }
    }
}

Output results:


key : Double value : class java.lang.Double
key : Integer value : class java.lang.Integer

Simple example: an example for 1.1


public static void main(String[] args) {
        //Test Integer generics
        Test<Integer> t1 = new Test<Integer>();
        t1.setValue(5);
        fun(t1);
        //Test for Double generics
        Test<Double> t2 = new Test<Double>();
        t2.setValue(5.55D);
        fun(t2);
    }
    public static void fun(Test<?> t){
        System.out.println(" Wildcard generic "+t.getValue());
    }

Output results:


java.lang.Integer
 Wildcard generic 5
java.lang.Double
 Wildcard generic 5.55

Added 6.

In generics, you might encounter < T> < E> T and E are the first letters of Type and Element, respectively. E is generally used to represent the type of an element in a collection type, such as the definition of the List interface, public interface List< E> Extends Collection< E> . This is just a naming convention, there is no essential difference between the two.


Related articles: