Java Generics and Collections, 2nd 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. Brief Table of Contents (Not Yet Final)
  2. 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
  3. 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
  4. 3. Declarations
    1. Constructors
    2. Static Members
    3. Nested Classes
    4. How Erasure Works
  5. 4. Effective Generics
    1. Use Checked Collections to Enforce Security
    2. Specialize to Create Reifiable Types
    3. Maintain Binary Compatibility
    4. Avoid Single-Use Type Variables
    5. Use Generic Helper Methods to Capture a Wildcard
    6. Cast Through Raw Types When Necessary
    7. Use Generic Array Types With Care
    8. Use Type Tokens for Operations Requiring Run-time Type Information
  6. 5. The Main Interfaces of the Java Collections Framework
    1. Using the Different Collection Types
    2. Sequenced Collections
  7. 6. 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
  8. 7. The Collection Interface
    1. Using the Methods of Collection
    2. Implementing Collection
    3. Collection Constructors
  9. 8. The SequencedCollection Interface
  10. 9. 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
  11. 10. 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
  12. 11. Lists
    1. Using the Methods of List
    2. List Implementations
      1. ArrayList
      2. LinkedList
      3. CopyOnWriteArrayList
    3. Comparing List Implementations
  13. 12. 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
  14. 13. 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
  15. 14. 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
  16. 15. Design Retrospective
    1. Fundamental Issues in the Framework Design
      1. Desirable Characteristics of a Collection Framework
      2. Unmodifiability via Subtyping
      3. Limitations of Subtyping
      4. Complementing Static Typing
      5. Lumping and Splitting
      6. Summary
    2. Nulls
    3. Inconsistent with equals
    4. Object vs. E

Product information

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