Get type of generic type inside a List in Java

前端 未结 7 2372
再見小時候
再見小時候 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:36

    You can not find the type of T as the type information is erased. Check this for more details. But if the list is not empty, you can get an element from the list and can find out using instanceof and if else

提交回复
热议问题