Java Generics and Collections, Second Edition

Book description

Java Generics and Collections has been the go-to guide to generics for more than a decade. This second edition covers Java 21, providing a clear guide to generics from their most common uses to the strangest corner cases, giving you everything you need to know to use and write generic APIs effectively. It covers the collections library thoroughly, so you'll always know how and when to use each collection for any given task. And it explains stream processing, so you'll know which model to use and how they interoperate to get the best out of the platform library.

This indispensable guide covers:

  • Fundamentals of generics: type parameters and generic methods
  • Subtyping and wildcards
  • Generics and reflection
  • Design patterns for generics
  • Sets, queues, lists, maps, and their implementations
  • Concurrent programming and thread safety with collections
  • Performance of different collection implementations
  • Best practices for using and extending the Java collections framework
  • Design philosophy and comparison with other collections libraries

Publisher resources

View/Submit Errata

Table of contents

  1. 1. Subtyping and Wildcards
    1. Subtyping and the Substitution Principle
    2. Wildcards with extends
    3. Wildcards with super
    4. The Get and Put Principle
    5. Arrays
    6. Wildcards Versus Type Parameters
    7. Wildcard Capture
    8. Restrictions on Wildcards
  2. 2. Comparison and Bounds
    1. Comparable<T>
    2. Maximum of a Collection
    3. A Fruity Example
    4. Comparator<T>
    5. Enumerated Types
    6. Multiple Bounds
    7. Bridges
    8. Covariant Overriding
  3. 3. Declarations
    1. Constructors
    2. Static Members
    3. Nested Classes
    4. How Erasure Works
  4. 4. The Main Interfaces of the Java Collections Framework
    1. Using the Different Collection Types
    2. Sequenced Collections
  5. 5. Preliminaries
    1. Iterable and Iterators
    2. Implementations
    3. Views
    4. Performance
      1. Memory
      2. Instruction Count and the O-notation
    5. Immutability and Unmodifiability
    6. Contracts
      1. Content-based Organization
    7. Lambdas and Streams
    8. Collections and Thread Safety
      1. Synchronization and the Legacy Collections
      2. Java 2: Synchronized Collections and Fail-Fast Iterators
      3. Java 5: Concurrent Collections
  6. 6. The Collection Interface
    1. Using the Methods of Collection
    2. Implementing Collection
    3. Collection Constructors
  7. 7. The SequencedCollection Interface
  8. 8. Sets
    1. Set Implementations
      1. HashSet
      2. CopyOnWriteArraySet
      3. EnumSet
      4. UnmodifiableSet
    2. SequencedSet
      1. LinkedHashSet
    3. NavigableSet
      1. TreeSet
      2. ConcurrentSkipListSet
    4. Comparing Set Implementations
  9. 9. Queues
    1. Using the Methods of Queue
    2. Queue Implementations
      1. PriorityQueue
      2. ConcurrentLinkedQueue
    3. BlockingQueue
      1. Using the Methods of BlockingQueue
      2. BlockingQueue Implementations
      3. TransferQueue
    4. Deques
      1. Deque Implementations
      2. BlockingDeque
    5. Comparing Queue Implementations
  10. 10. Lists
    1. Using the Methods of List
    2. List Implementations
      1. ArrayList
      2. LinkedList
      3. CopyOnWriteArrayList
    3. Comparing List Implementations
  11. 11. Maps
    1. Map.Entry
    2. Using the Methods of Map
    3. Map Implementations
      1. HashMap
      2. WeakHashMap
      3. IdentityHashMap
      4. EnumMap
      5. UnmodifiableMap
    4. SequencedMap
      1. LinkedHashMap
    5. NavigableMap
      1. TreeMap
    6. ConcurrentMap
      1. ConcurrentHashMap
    7. ConcurrentNavigableMap
      1. ConcurrentSkipListMap
    8. Comparing Map Implementations
  12. 12. The Collections Class
    1. Generic Algorithms
      1. Changing the Order of List Elements
      2. Changing the Contents of a List
      3. Finding Extreme Values in a Collection
      4. Finding Specific Values in a List
    2. Collection Factories
    3. Wrappers
      1. Synchronized Collections
      2. Unmodifiable Collections
      3. Checked Collections
    4. Other Methods
  13. 13. Guidance for Using the Java Collections Framework
    1. Avoid Anemic Domain Models
    2. Respect the “Ownership” of Collections
    3. Prefer Immutable Objects as Set Elements or Map Keys
    4. Balance Client and Library Interests in API Design
    5. Exploit the Features of Records
      1. Prefer Records to Parallel Lists
      2. Use Records as Composite Keys
      3. Aim for Immutability of Records
    6. Avoid Legacy Implementations
      1. Avoid synchronized wrapper collections
      2. Avoid LinkedList
    7. Customize Collections Using the Abstract Classes

Product information

  • Title: Java Generics and Collections, Second Edition
  • Author(s): Maurice Naftalin, Philip Wadler
  • Release date: June 2025
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098136727