Get type of generic type inside list in java

I have the following 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 a String or Integer, so I wonder if there is a way to open its type? I used the code above, but it generated an error

Thanks in advance.

+7
java generics
source share
7 answers

You cannot find type T as type information is deleted. See this for more details. But if the list is not empty, you can get the item from the list and find out using instanceof and if else

+8
source share

This is not possible in Java due to erasure . Most people add a type token instead. Example:

 public <T> void putList(String key, List<T> list, Class<T> listElementType) { } 

There are certain situations in which reflection can be obtained in the type parameter, but this is for cases when you have previously set the type parameter. For example:

 public class MyList extends List<String> { private List<String> myField; } 

In both cases, reflection can determine that List is of type String, but reflection cannot determine it for your case. You will have to use a different approach, such as a type marker.

+9
source share

This cannot be determined due to erasure , which means that this parameter is not stored in the code. However, you can either pass an additional parameter that determines the type of list:

 public <T> void putList(String key, List<T> lst, Class<T> listElementType) { 

}

or you can determine the type of each element at runtime:

 public <T> void putList(String key, List<T> lst){ for (Object elem:lst) { if (elem instanceof String) { // Do something } if (elem instanceof Integer) { // Do something } } } 
+1
source share

Generics are called "erasures" because they exist only at compile time and are removed by the compiler. Thus, there is no way to determine the general type of collection. The only way to do this for non-empty lists is to take the first element and determine its type.

This is almost the same solution that DJClayworth suggested, but I think there is no need to check every item in the list. If you are sure that the list was created using generics (new ArrayList () or new LinkedList (), etc.), all its elements are guaranteed to be of the same type.

+1
source share

If you want to run an object function, you can do:

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

Use the instanceof operator.

However, general operations should be, well, general, so be careful when changing type-based behavior.

-one
source share
 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); } } 
-one
source share

All Articles