I’m using generics rather long time but I’ve never used construction like List<? super T>.
What does it mean? How to use it? How does it look after erasure?
I also wonder: is it something standard in generic programming (template programming?) or it’s just a java ‘invention’? Does c#, for example, allow similar constructions?
This construct is used when you want to consume items from a collection into another collection. E.g. you have a generic
Stackand you want to add apopAllmethod which takes a Collection as parameter, and pops all items from the stack into it. By common sense, this code should be legal:but it compiles only if you define
popAlllike this:The other side of the coin is that
pushAllshould be defined like this:Update: Josh Bloch propagates this mnemonic to help you remember which wildcard type to use:
PECS stands for producer-extends, consumer-super.
For more details, see Effective Java 2nd Ed., Item 28.