Get the first element in your List
(assuming is not empty) and test the type of the element retrieved. You may use instanceof
operator to see if the object is in the class hierarchy of the desired class, but if you want a more accurate test, use getClass().equals(YourClass.class)
.
public void foo(List<Object> list) {
if (!list.isEmpty()) {
Object object = list.get(0);
//using instanceof
if (object instanceof Cat) {
}
if (object instanceof Orange) {
}
//using getClass().equals
if (object.getClass().equals(Cat.class)) {
}
if (object.getClass().equals(Orange.class)) {
}
}
}