How do I do this? Because you can only extend one class so it can only have one upper bound.
In my case I need the generic type to be bounded in String and int. If I use an Integer wrapper instead of int and rely on auto-boxing, I can make it but the problem is other classes can be passed as a type parameter as well.
What’s the best way to do this?
You could use the non generic variants of collections (e.g List), or
more cleanly explicitly
List<Object>to show code’s intention.Wrap that in a MyList class, and create add(), get() methods for each type you want to support:
But Object get() cannot be typed, such that it makes sense.
So finally you also can use Object with List, and omit the wrapper.