For maximum flexibility, use wildcard types on input parameters that represent producers or consumers.
Mnemonic:
PECS stands for producer-extends, consumer-super.
E.g., method that takes a sequence of elements and pushes them all onto the stack:
1 2 3 4 5 6 | // pushAll method without wildcard type - deficient! public void pushAll(Iterable<E> src) { for (E e : src) { push(e); } } |
1 2 3 4 5 6 | // Wildcard type for parameter that serves as an E producer public void pushAll(Iterable<? extends E> src) { for (E e : src) { push(e); } } |
1 2 3 4 5 6 | // popAll method without wildcard type - deficient! public void popAll(Collection<E> dst) { while (!isEmpty()) { dst.add(pop()); } } |
1 2 3 4 5 6 | // Wildcard type for parameter that serves as an E consumer public void popAll(Collection<? super E> dst) { while (!isEmpty()) { dst.add(pop()); } } |