Generics do not allow differentiation. The type erasure will play its tricks.. I would suggest you to write a custom logic like the below:
public class Test {
public boolean accept(List<? extends A> list) {
for (Object o : list) {
if (o instanceof E && !(o instanceof F))
return true;
else if (o instanceof C && !(o instanceof D) && !(o instanceof F))
return true;
else if (o instanceof A && !(o instanceof B) && !(o instanceof D) && !(o instanceof F))
return true;
return false;
}
return false;
}
}
class A {
}
class B extends A {
}
class C extends B {
}
class D extends C {
}
class E extends D {
}
class F extends E {
}
Test Method to test the above code:
public static void main(String [] args){
Test t = new Test();
List<F> listF = new ArrayList<F>();
listF.add(new F());
System.out.println("List of F returns "+ t.accept(listF));
List<E> listE = new ArrayList<E>();
listE.add(new E());
System.out.println("List of E returns "+ t.accept(listE));
List<D> listD = new ArrayList<D>();
listD.add(new D());
System.out.println("List of D returns "+ t.accept(listD));
List<C> listC = new ArrayList<C>();
listC.add(new C());
System.out.println("List of C returns "+ t.accept(listC));
List<B> listB = new ArrayList<B>();
listB.add(new B());
System.out.println("List of B returns "+ t.accept(listB));
List<A> listA = new ArrayList<A>();
listA.add(new A());
System.out.println("List of A returns "+ t.accept(listA));
}
Output of the test:
List of F returns false
List of E returns true
List of D returns false
List of C returns true
List of B returns false
List of A returns true
EDIT:
If you want to do some more research, this might help you . Get generic type of java.util.List