Generics and collections

This part starts with the type parameter: the diamond, a bounded parameter, a generic method with ? extends and ? super, and type erasure, which is why new T[] is refused. The collections follow: List with ArrayList, the algorithms over it and the ConcurrentModificationException of a remove in a loop, Set in its three orders, Map with merge and entrySet, grouping and counting, Deque as a stack and as a queue, Comparable and Comparator, the Collections utilities, iteration with an iterator. A word index capstone closes it, next to the mutable key that disappears from a HashMap and the table that says which collection fits.

6.1 Generic classes a type parameter, the diamond, a bounded parameter, a generic pair Open 6.2 Generic methods and wildcards a generic method, ? extends, ? super, PECS, Collections.copy Open 6.3 Type erasure what the compiler erases, getClass on a list, why new T[] is refused, Class<T> Open 6.4 List and ArrayList add, get, set, remove, size, contains, ArrayList against LinkedList Open 6.5 Lists in practice Collections.sort, subList, an Iterator, ConcurrentModificationException Open 6.6 Set HashSet, LinkedHashSet, TreeSet, uniqueness, the order each keeps Open 6.7 Map put, get, getOrDefault, merge, keySet, values, entrySet Open 6.8 Grouping and counting computeIfAbsent, a map of lists, counting, sorting by value Open 6.9 Deque and ArrayDeque a stack, a queue, push and pop, offer and poll, peek Open 6.10 Comparable compareTo, natural order, a record that implements it, Arrays.sort Open 6.11 Comparator a lambda comparator, thenComparing, reversed, nullsLast, a sort by two keys Open 6.12 Collections and Arrays utilities unmodifiableList, emptyList, frequency, shuffle, max, asList and its limit Open 6.13 Iterating and removing for-each, Iterator.remove, ListIterator, the fail-fast check Open 6.14 Capstone: a word index a Map of lists, a sorted index, the output checked by hand Open 6.15 Keys, equals and hashCode a mutable key that disappears, a HashMap miss, the fix Open 6.16 Choosing a collection the cost table, when each of List, Set, Map and Deque fits Open

Lessons