Java Reference ยท Collections
Collections and generics
How generics type Java collections: parameterized types, the diamond operator, wildcards, PECS, raw types and why List of Integer is not a List of Number.
Every collection in java.util is generic: List<E>, Set<E>, Map<K,V>. The type arguments tell the compiler what the collection holds, so list.get(0) returns a String without a cast, and list.add(42) on a List<String> is rejected at compile time rather than failing later with a ClassCastException.
Generics are a compile-time feature. After compilation the type arguments are erased: at run time a List<String> and a List<Integer> are both just List. That is why you cannot write new T[10], instanceof List<String> or overload two methods that differ only in type arguments. This page covers the rules you need when working with typed collections.
List<String> names = new ArrayList<>(); // diamond: type inferred Map<String, List<Integer>> index = new HashMap<>();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Concepts
| Notation | Meaning | You can |
|---|---|---|
List<String> |
Exactly strings. | Read and add String. |
List<? extends Number> |
A list of some subtype of Number. |
Read as Number; add only null. |
List<? super Integer> |
A list of Integer or a supertype. |
Add Integer; read only as Object. |
List<?> |
A list of anything. | Read as Object; add only null. |
List (raw) |
Pre-generics type. | Everything, unchecked. Avoid. |
PECS
"Producer extends, Consumer super": use ? extends T for a parameter you only read from (it produces values), and ? super T for one you only write to (it consumes values). Collections.copy(dest, src) is declared as copy(List<? super T> dest, List<? extends T> src) for exactly this reason.
Diamond and inference
new ArrayList<>() infers its type arguments from the target type (Java 7). Since Java 9 the diamond also works with anonymous classes. var list = new ArrayList<String>(); (Java 10) infers the variable type from the right-hand side, so there the type argument must be written.
Usage notes
- Invariance protects you: if
List<Integer>were aList<Number>, you could add aDoubleto it. Arrays are covariant and checked at run time instead (ArrayStoreException). - Primitives are not allowed as type arguments. Use
List<Integer>, which boxes each value; for large numeric data preferint[]or a primitive stream. - No generic arrays:
new List<String>[5]does not compile. UseList<List<String>>orArrayList<String>[]with an unchecked cast. - Heap pollution: mixing raw and generic code lets wrong types in. The compiler warns ("unchecked"); treat these warnings as bugs.
- Generic methods declare their own type parameter before the return type:
static <T> List<T> listOf(T a, T b). List<Object>is notList<?>: the first accepts any object but only that exact type; the second accepts any list but you can read onlyObject.- Runtime type checks need a
Class<T>token, e.g.Collections.checkedList(list, String.class).