I need to accept a Map that can be either TypeA or TypeB like below.
Map<Animal, List<TypeA/TypeB>>
How do I declare this? I cannot specify two classes for upper bounds.
Map<Animal, List<? extends Object>> OR Map<Animal, List<Object>>
is too wide. I wanted to restrict it to TypeA or TypeB
Any better way?
Cannot wrap TypeA and TypeB with interface.
EDIT:
From the answers, I understand that, it is not possible with custom types.
The follow up questions is, if I just use <? extends Object>
, how should I check the type of ? is either TypeA or TypeB. Should I use instanceof? Code sample?