PECS
A rule of thumb for generic wildcards: a collection that produces values for you uses extends, and one that consumes values from you uses super.
In more detail
PECS stands for producer extends, consumer super. It follows from how the compiler protects collections: you may read from an unknown subtype and write to an unknown parent type. If a method both reads and writes, use an exact type instead of a wildcard.
Related terms
- generic wildcard — The question mark in a generic type, as in a list of some unknown subtype of Number, meaning some specific type that this code does not know.
- bounded type parameter — A type parameter restricted to types that support a capability, such as T extends Comparable<T>, so the code can call that capability.
Taught in
- Generics And Collections — Modern Java: Virtual Threads & High-Throughput Services