how to instanceof List<MyType>?

That is not possible because the datatype erasure at compile time of generics. Only possible way of doing this is to write some kind of wrapper that holds which type the list holds:

public class GenericList <T> extends ArrayList<T>
{
     private Class<T> genericType;

     public GenericList(Class<T> c)
     {
          this.genericType = c;
     }

     public Class<T> getGenericType()
     {
          return genericType;
     }
}

if(!myList.isEmpty() && myList.get(0) instanceof MyType){
    // MyType object
}

You probably need to use reflection to get the types of them to check. To get the type of the List: Get generic type of java.util.List

Tags:

Java

Generics