Java 获取List的泛型类型

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class Test {

    public static void main(String[] args) throws NoSuchFieldException {
        Class clazz = A.class;
        Field field = clazz.getField("list");
        ParameterizedType type = (ParameterizedType) field.getGenericType();
        Stream.of(type.getActualTypeArguments()).forEach(System.out::println);

        field = clazz.getField("list1");
        type = (ParameterizedType) field.getGenericType();
        Stream.of(type.getActualTypeArguments()).forEach(System.out::println);
    }

    class A {
        public List list = new ArrayList<>();

        public List list1 = new ArrayList<>();
    }

}

结果:

extends java.lang.String
class java.lang.Integer

你可能感兴趣的:(java,java,list,jvm,大数据,javascript)