Lesson · Generics
PECS: read with extends, write with super
15 min readPECS
Producer Extends, Consumer Super: the two keywords that decide a parameter's wildcard. The traffic of a method — what it reads, what it writes — is exactly what the signature must promise.
The signature that reads like a sentence
This is the real signature of Collections.copy in the Java standard library:
public static <T> void copy(List<? super T> dest, List<? extends T> src) // copy INTO a destination that can hold any T (super) // FROM a source that produces T (extends)
Predict: why the two different keywords on one line? Because the two lists have opposite traffic: the source only gives values out, and the destination only takes values in. Each keyword matches its side's job.
One sentence: read → extends, write → super
The rule has a famous name — PECS: Producer Extends, Consumer Super — and it is decided per parameter, by what the body does with it:
| The parameter is | Keyword | What it promises |
|---|---|---|
| A source the body only reads from (a producer) | ? extends T | every element is at least a T; nothing is ever put in |
| A destination the body only writes into (a consumer) | ? super T | every list that can hold a T is accepted; nothing is read out |
| Both read and written | T exactly | the operation needs the same type on both sides |
- Plain reading: values coming out of it →
extends; values going into it →super. - The body decides the keyword — not taste, not style: the wildcard is the widest promise the body can honestly keep.
Why 'super' is the destination's word
The copy method from the opener, in code:
static <T> void copyInto(List<? super T> destination, List<? extends T> source) { for (T item : source) { // read: source produces T destination.add(item); // write: destination consumes T } }
List<? super Integer>admitsList<Integer>,List<Number>andList<Object>— every list that can hold an Integer.- Inside,
destination.add(item)compiles: all of those lists accept a T. - Reading it is deliberately useless:
destination.get(0)gives onlyObject— the body must not depend on the element type. - The two refusals: adding to a
? extendsparameter fails withT cannot be converted to CAP#1; reading a T out of a? superparameter fails withCAP#1 cannot be converted to Number— each keyword cuts off the direction it cannot promise.
Two refusals, one shape. extends refuses to write; super refuses to read. Between them they cover everything a method can legally promise about traffic.
PECS lane · read, write, or both?
pick a keyword for every parameterfor (T item : src) {
dest.add(item);
}
Producer Extends, Consumer Super — each keyword matches one direction of traffic.
The pattern in the standard library
The library writes PECS signatures everywhere the traffic is one-way:
Collections.copy(List<? super T> dest, List<? extends T> src)— today's method.Collections.addAll(Collection<? super T> c, T... elements)— a destination: consumer, sosuper.Collections.min(Collection<? extends T> coll)— a source: producer, soextends.List.sort(Comparator<? super E> c)— the comparator consumes elements, sosuper.Stream.map(Function<? super T, ? extends R> mapper)— PECS inside one type: the function consumes a T (super) and produces an R (extends).
When NOT to use wildcards
Wildcards are for positions you accept values in — parameters. Two clear cases where they must not appear:
- Both directions in one parameter? Plain T.
static <T> void swap(List<T> list, int i, int j)reads and writes the same list — a wildcard would block one of the two. - Return types are never wildcards. A method that produces a T returns
T(orList<T>); the caller deserves the exact promise, not 'some subtype'.
Parameters accept, returns promise. ? softens what you take; it must never soften what you hand back.
The PECS playbook
For every generic parameter a method accepts:
- Write the method with a plain
Tfirst and make the body work. - Look at the parameter's traffic: out →
? extends T; in →? super T; both → leaveT. - Check the callers you expect: a
List<Integer>must fit a? super Integerdestination. - Never wildcard the return type; never wildcard a parameter the body both reads and writes.
The keyword is a consequence. You do not decorate a signature with PECS — you read what the body does and the keyword follows.
PECS — Producer Extends, Consumer Super
A parameter that produces values for the method takes ? extends T; one that consumes the method's values takes ? super T; one used both ways takes plain T. It is not decoration: it is the widest signature the body can honestly support, and the standard library has written copy, addAll, min, sort and map that way since Java 5.
Interview angles
What does PECS stand for, and what does it decide?+
Producer Extends, Consumer Super: a parameter the method reads from gets ? extends T; one it writes to gets ? super T.
The traffic decides the wildcard, and the wildcard decides which callers fit. An extends-source accepts any list of T or a subtype; a super-destination accepts any list that can hold a T — each is the widest safe promise for its direction.
Common misconception: That the keywords are stylistic and either compiles somewhere — each keyword is exactly one direction of traffic.
Follow-up: And a parameter the body both reads and writes?
Why can't you add to a List<? extends Number>?+
The element type is some unknown subtype of Number. The compiler refuses to guess: adding an Integer could corrupt a List<Double> that the wildcard also admits.
The error names the unknown: int cannot be converted to CAP#1, where CAP#1 is the captured wildcard type. It is not a missing feature — it is the promise of extends being enforced.
Common misconception: That extends makes the list read-only because the list is immutable — the list is mutable; the static type refuses the call.
Follow-up: Where do values go if you need to write them somewhere?
Why can't a List<? super Integer> hand you an Integer when you read?+
Because it might be a List<Object>: get() is only guaranteed to return Object, and the body must not assume more.
Super keeps reading open at the Object level (size, clear, removing) but refuses Integer i = list.get(0) with CAP#1 cannot be converted to Integer. The destination side of an operation never needs to read the old values — the copy loop proves it: it only calls add.
Common misconception: That super means the list is 'more capable' and reads fine — it trades reading for writing.
Follow-up: Which is exactly the trade copy's destination makes — why is that safe there?
Walk through Collections.copy's signature using PECS.+
dest is List<? super T> — a consumer the body only adds into; src is List<? extends T> — a producer the body only reads from. Each keyword matches the traffic of its side.
Read it as a sentence: copy T values from anything that produces T into anything that accepts T. The exact type T still links the two sides — the elements that come out of src must be assignable into dest.
Common misconception: That the two wildcards are independent conveniences — they are the two directions of one flow, tied by the shared T.
Follow-up: Could copy be written with plain T on both sides? What would callers lose?
Why does Stream.map take Function<? super T, ? extends R>?+
PECS inside a function type: the function consumes an input (super, so any consumer of T works) and produces an output (extends, so any subtype of R satisfies the caller).
The same rule you would apply to two separate parameters applies to the two positions of the function type. A Function<Object, String> is a valid mapper for a Stream<Number>: it consumes Numbers as Objects, and produces Strings as R.
Common misconception: That the wildcards on map are special-cased by the library — they are ordinary PECS applied to a type with two slots.
Follow-up: What would Function<T, R> reject that Function<? super T, ? extends R> accepts?