Get type of generic type inside a List in Java

前端 未结 7 2272
天命终不由人
天命终不由人 2021-02-15 23:01

I have the below function:


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


        
7条回答
  •  迷失自我
    2021-02-15 23:56

    If you want to run function of the object, you can do:

    public  void putList(String key, List lst){
        for(T object : lst){
             if(object instanceof String) {
                 doSomething(((String)object).doForString())
             }
             if(object instanceof Integer) {
                 doSomething(((Integer)object).doForInteger())
             }
        }
    }
    

提交回复
热议问题