Sub topic 14
Choosing Wildcards with PECS
You have now seen two kinds of wildcards. ? extends T lets you read from a collection. ? super T lets you write to one. This chapter gives you a simple rule for picking the right one each time.
The PECS Rule
PECS stands for Producer Extends, Consumer Super. It is a memory aid that Joshua Bloch made popular in his book Effective Java.
- A producer is a parameter that gives values to your method. Your code reads from it. Use
? extends T. - A consumer is a parameter that takes values from your method. Your code writes into it. Use
? super T. - If a parameter does both, do not use a wildcard. Use the plain type, such as
List<T>.
To decide, ask one question about each parameter: "Does my method take things out of it, or put things into it?"
A Worked Example: Copying a List
A copy method shows both halves of the rule. It reads from one list and writes into another.
import java.util.ArrayList;
import java.util.List;
public class PecsDemo {
// src produces T values, so it uses extends.
// dst consumes T values, so it uses super.
static <T> void copy(List<? extends T> src, List<? super T> dst) {
for (T item : src) {
dst.add(item);
}
}
public static void main(String[] args) {
List<Integer> ints = List.of(1, 2, 3);
List<Number> numbers = new ArrayList<>();
List<Object> objects = new ArrayList<>();
copy(ints, numbers);
copy(ints, objects);
System.out.println(numbers); // [1, 2, 3]
System.out.println(objects); // [1, 2, 3]
}
}The source list src is a producer, because the loop reads items out of it. The target list dst is a consumer, because add puts items into it. With both wildcards in place, the method copies Integer values into a List<Number> and also into a List<Object>. If both parameters were plain List<T>, neither call would compile, because both lists would need the exact same element type.
Quick Reference
| What the method does with the parameter | Role | Wildcard | Example |
|---|---|---|---|
| Reads values out of it | Producer | `? extends T` | `sum(List<? extends Number>)` |
| Writes values into it | Consumer | `? super T` | `fill(List<? super Integer>)` |
| Reads and writes | Both | none (`List<T>`) | `swap(List<T>, int, int)` |
| Does not touch the elements | Neither | `?` | `size(List<?>)` |
Exercises
Exercise
A method printAll(List<??> items) loops over the list and prints each Number it finds. It never adds anything. Which wildcard should replace ???
Show answer
? extends Number. The list is a producer, because the method only reads Numbers out of it. So the method accepts List<Integer>, List<Double>, and List<Number>.
Exercise
A method addZeros(List<??> target, int count) adds count Integer zeros to the list. Which wildcard fits, and why?
Show answer
? super Integer. The list is a consumer, because the method only puts Integers into it. So it accepts List<Integer>, List<Number>, and List<Object>.
Exercise
A method reverses a list in place. It reads elements and writes them back. Should it use ? extends T, ? super T, or List<T>?
Show answer
List<T>. The list is both a producer and a consumer, so PECS says to use the exact type with no wildcard.