Get type of generic type inside a List in Java

前端 未结 7 2394
再見小時候
再見小時候 2021-02-15 22:58

I have the below function:


    public  void putList(String key, List lst){
          if (T instanceof String) {
          // Do something              


        
相关标签:
7条回答
  • 2021-02-15 23:57
    Type genericType = lst.getType();
    
    if(genericType instanceof ParameterizedType){
        ParameterizedType aType = (ParameterizedType) genericType;
        Type[] fieldArgTypes = aType.getActualTypeArguments();
        for(Type fieldArgType : fieldArgTypes){
            Class fieldArgClass = (Class) fieldArgType;
            System.out.println("fieldArgClass = " + fieldArgClass);
        }
    }
    
    0 讨论(0)
提交回复
热议问题