Sub topic 12
Reading with ? extends
In the last chapters you saw that a List<Integer> is not a List<Number>. That rule keeps your lists safe, but it can make methods less useful. This chapter shows how ? extends fixes that problem when you only need to read from a list.
The Problem
Say you want one method that adds up any list of numbers. You might write this:
static double total(List<Number> nums) { ... }This method accepts a List<Number>, but it rejects a List<Integer> or a List<Double>. That is a shame, because every Integer and every Double is a Number.
The Fix: An Upper-Bounded Wildcard
A wildcard is the ? symbol in a type. It means "some type I don't name." An upper-bounded wildcard is a wildcard with a limit on top. List<? extends Number> means "a list of some type that is Number or a subclass of Number."
A Worked Example
import java.util.List;
public class ExtendsDemo {
static double total(List<? extends Number> nums) {
double sum = 0;
for (Number n : nums) {
sum += n.doubleValue();
}
return sum;
}
public static void main(String[] args) {
List<Integer> ints = List.of(1, 2, 3);
List<Double> doubles = List.of(1.5, 2.5);
System.out.println(total(ints)); // 6.0
System.out.println(total(doubles)); // 4.0
}
}Each item you read out of nums is safe to treat as a Number. Whatever the real type is, it must be Number or a subclass, so doubleValue() always works.
Why You Cannot Add
Reading is safe. Adding is not. Look at this method:
static void broken(List<? extends Number> nums) {
// nums.add(5); // compile error
// nums.add(2.5); // compile error
}The compiler doesn't know which list it will get. If the caller passes a List<Double>, adding 5 would put an Integer into a list of Doubles. The compiler can't rule that out, so it blocks every add call. The one exception is null, which fits any type.
Quick Reference
| Action on `List<? extends Number>` | Allowed? | Why |
|---|---|---|
| `Number n = list.get(0);` | Yes | Every element is some kind of `Number` |
| `list.add(5);` | No | The list might hold only `Double`s |
| `list.add(null);` | Yes | `null` fits any type |
| `list.size()` | Yes | Does not depend on the element type |
When to Use It
Use ? extends T when a method only reads values out of a collection. The collection gives the method data, so the method can accept lists of many related types. The next chapter covers the opposite case, where a method needs to put values in.
Exercise
You write static void show(List<? extends Number> list). Inside it, which line compiles: Number x = list.get(0); or list.add(Integer.valueOf(7));? Explain why.
Show answer
Number x = list.get(0); compiles because every element is a Number or a subclass. list.add(Integer.valueOf(7)); does not compile, because the list could really be a List<Double>, and an Integer doesn't belong there.
Exercise
Can you pass a List<Object> to a method whose parameter is List<? extends Number>?
Show answer
No. Object is a superclass of Number, not a subclass. ? extends Number only accepts Number or a subclass of Number, such as Integer or Double.