dimanche 13 août 2017

java get type of a List's each element

I have

public interface Entity {}

public class A implements Entity {}

public class B implements Entity {}


public interface Event<T extends Entity> {}

public class EventA implements Event<A> {}

public class EventB implements Event<B> {}

If I have a list of Events:

List<Event<?>> events = new ArrayList<>();
events.add(new EventA());
events.add(new EventB());

When I iterate events, how do I get each Event's type?

Say type of events.get(0) is A, type of events.get(1) is B.





Aucun commentaire:

Enregistrer un commentaire