O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java Generics and Collections

Book Description

This comprehensive guide shows you how to master the most importantchanges to Java since it was first released. Generics and the greatlyexpanded collection libraries have tremendously increased the power ofJava 5 and Java 6. But they have also confused many developers whohaven't known how to take advantage of these new features.

Java Generics and Collections covers everything from the mostbasic uses of generics to the strangest corner cases. It teaches youeverything you need to know about the collections libraries, so you'llalways know which collection is appropriate for any given task, andhow to use it.

Topics covered include:

  • Fundamentals of generics: type parameters and generic methods
  • Other new features: boxing and unboxing, foreach loops, varargs
  • Subtyping and wildcards
  • Evolution not revolution: generic libraries with legacy clients andgeneric clients with legacy libraries
  • Generics and reflection
  • Design patterns for generics
  • Sets, Queues, Lists, Maps, and their implementations
  • Concurrent programming and thread safety with collections
  • Performance implications of different collections

Generics and the new collection libraries they inspired take Java to anew level. If you want to take your software development practice toa new level, this book is essential reading.

Philip Wadler is Professor of Theoretical Computer Science at theUniversity of Edinburgh, where his research focuses on the design ofprogramming languages. He is a co-designer of GJ, work thatbecame the basis for generics in Sun's Java 5.0.

Maurice Naftalin is Technical Director at Morningside Light Ltd., a software consultancy in the United Kingdom. He has most recently served as an architect and mentor at NSB Retail Systems plc, and as the leader of the client development team of a major UK government social service system.

"A brilliant exposition of generics. By far the best book on thetopic, it provides a crystal clear tutorial that starts with thebasics and ends leaving the reader with a deep understanding of boththe use and design of generics."
Gilad Bracha, Java Generics Lead, Sun Microsystems

Table of Contents

  1. Dedication
  2. A Note Regarding Supplemental Files
  3. Preface
    1. Obtaining the Example Programs
    2. How to Contact Us
    3. Conventions Used in This Book
    4. Using Code Examples
    5. Safari® Books Online
    6. Acknowledgments
  4. I. Generics
    1. 1. Introduction
      1. Generics
      2. Boxing and Unboxing
      3. Foreach
      4. Generic Methods and Varargs
      5. Assertions
    2. 2. 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. 3. Comparison and Bounds
      1. Comparable
      2. Maximum of a Collection
      3. A Fruity Example
      4. Comparator
      5. Enumerated Types
      6. Multiple Bounds
      7. Bridges
      8. Covariant Overriding
    4. 4. Declarations
      1. Constructors
      2. Static Members
      3. Nested Classes
      4. How Erasure Works
    5. 5. Evolution, Not Revolution
      1. Legacy Library with Legacy Client
      2. Generic Library with Generic Client
      3. Generic Library with Legacy Client
      4. Legacy Library with Generic Client
        1. Evolving a Library using Minimal Changes
        2. Evolving a Library using Stubs
        3. Evolving a Library using Wrappers
      5. Conclusions
    6. 6. Reification
      1. Reifiable Types
      2. Instance Tests and Casts
      3. Exception Handling
      4. Array Creation
      5. The Principle of Truth in Advertising
      6. The Principle of Indecent Exposure
      7. How to Define ArrayList
      8. Array Creation and Varargs
      9. Arrays as a Deprecated Type?
      10. Summing Up
    7. 7. Reflection
      1. Generics for Reflection
      2. Reflected Types are Reifiable Types
      3. Reflection for Primitive Types
      4. A Generic Reflection Library
      5. Reflection for Generics
      6. Reflecting Generic Types
    8. 8. Effective Generics
      1. Take Care when Calling Legacy Code
      2. Use Checked Collections to Enforce Security
      3. Specialize to Create Reifiable Types
      4. Maintain Binary Compatibility
    9. 9. Design Patterns
      1. Visitor
      2. Interpreter
      3. Function
      4. Strategy
      5. Subject-Observer
  5. II. Collections
    1. 10. The Main Interfaces of the Java Collections Framework
    2. 11. Preliminaries
      1. Iterable and Iterators
      2. Implementations
      3. Efficiency and the O-Notation
      4. Contracts
      5. Collections and Thread Safety
        1. Synchronization and the Legacy Collections
        2. JDK 1.2: Synchronized Collections and Fail-Fast Iterators
        3. Concurrent Collections: Java 5 and Beyond
    3. 12. The Collection Interface
      1. Using the Methods of Collection
      2. Implementing Collection
      3. Collection Constructors
    4. 13. Sets
      1. Implementing Set
        1. HashSet
        2. LinkedHashSet
        3. CopyOnWriteArraySet
        4. EnumSet
      2. SortedSet and NavigableSet
        1. NavigableSet
        2. TreeSet
        3. ConcurrentSkipListSet
      3. Comparing Set Implementations
    5. 14. Queues
      1. Using the Methods of Queue
      2. Implementing Queue
        1. PriorityQueue
        2. ConcurrentLinkedQueue
      3. BlockingQueue
        1. Using the Methods of BlockingQueue
        2. Implementing BlockingQueue
          1. LinkedBlockingQueue
          2. ArrayBlockingQueue
          3. PriorityBlockingQueue
          4. DelayQueue
          5. SynchronousQueue
      4. Deque
        1. Implementing Deque
          1. ArrayDeque
          2. LinkedList
        2. BlockingDeque
          1. Implementing BlockingDeque
      5. Comparing Queue Implementations
    6. 15. Lists
      1. Using the Methods of List
      2. Implementing List
        1. ArrayList
        2. LinkedList
        3. CopyOnWriteArrayList
      3. Comparing List Implementations
    7. 16. Maps
      1. Using the Methods of Map
      2. Implementing Map
        1. HashMap
        2. LinkedHashMap
        3. WeakHashMap
        4. IdentityHashMap
        5. EnumMap
      3. SortedMap and NavigableMap
        1. NavigableMap
        2. TreeMap
      4. ConcurrentMap
        1. ConcurrentHashMap
      5. ConcurrentNavigableMap
        1. ConcurrentSkipListMap
      6. Comparing Map Implementations
    8. 17. 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
  6. Index
  7. About the Authors
  8. Colophon
  9. Copyright