I have the below function:
public <T> void putList(String key, List<T> lst){
if (T instanceof String) {
// Do something
}
if (T instanceof Integer) {
// Do something
}
}
Inside this function, i want to know if <T> is String or Integer so i wonder if there is a way to discover its type? I used the above code but it generated error
Thank you in advance.
You can not find the type of
Tas 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 usinginstanceofandif else