Java's generic <T> T and T usage

<T> T indicates that the return value is a generic type. What type of data is returned, and a single T indicates that when the current object is created, the parameter type you pass is limited. In the following case, a generic type is passed The return method of to get the first data in each collection is realized by two methods of returning values ​​<T> T and T.

1. <T>T usage

This <T> T means that the return value T is generic, and T is a placeholder to tell the compiler that this thing will be kept for me first, and I will tell you when I compile it.

package  xxxx;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.formula.functions.T;

public class Demo {
    
    

    public static void main(String[] args) {
    
    

        Demo demo = new Demo();

        //获取string类型
        List<String> array = new ArrayList<String>();
        array.add("test");
        array.add("doub");
        String str = demo.getListFisrt(array);
        System.out.println(str);

        //获取nums类型
        List<Integer> nums = new ArrayList<Integer>();
        nums.add(12);
        nums.add(13);

        Integer num = demo.getListFisrt(nums);
        System.out.println(num);
    }

    /**
     * 这个<T> T 可以传入任何类型的List
     * 参数T
     *     第一个 表示是泛型
     *     第二个 表示返回的是T类型的数据
     *     第三个 限制参数类型为T
     * @param data
     * @return
     */
    private <T> T getListFisrt(List<T> data) {
    
    
        if (data == null || data.size() == 0) {
    
    
            return null;
        }
        return data.get(0);
    }

}

2. T usage
Return value, write T directly to indicate the type of restricted parameter. This method is generally used to jointly operate a class object and then obtain the collection information inside.

package xxxxxx;

import java.util.ArrayList;
import java.util.List;

public class Demo2<T> {
    
    

    public static void main(String[] args) {
    
    

        //限制T 为String 类型
        Demo2<String> demo = new Demo2<String>();

        //获取string类型
        List<String> array = new ArrayList<String>();
        array.add("test");
        array.add("doub");
        String str = demo.getListFisrt(array);
        System.out.println(str);

        //获取Integer类型 T 为Integer类型
        Demo2<Integer> demo2 = new Demo2<Integer>();
        List<Integer> nums = new ArrayList<Integer>();
        nums.add(12);
        nums.add(13);
        Integer num = demo2.getListFisrt(nums);
        System.out.println(num);
    }

    /**
     * 这个只能传递T类型的数据
     * 返回值 就是Demo<T> 实例化传递的对象类型
     * @param data
     * @return
     */
    private T getListFisrt(List<T> data) {
    
    
        if (data == null || data.size() == 0) {
    
    
            return null;
        }
        return data.get(0);
    }
}

To sum up a sentence:
The getListFisrt method of the first usage Demo class can accept any type of List parameters, and the
second Demo class has limited the type of T when it is initialized, so the getListFirst method can only accept List type parameters.

Guess you like

Origin blog.csdn.net/wujian_csdn_csdn/article/details/112735612