logo资料库

Java泛型和集合.pdf

第1页 / 共286页
第2页 / 共286页
第3页 / 共286页
第4页 / 共286页
第5页 / 共286页
第6页 / 共286页
第7页 / 共286页
第8页 / 共286页
资料共286页,剩余部分请下载后查看
Table of Contents
Preface
Obtaining the Example Programs
How to Contact Us
Conventions Used in This Book
Using Code Examples
Safari® Books Online
Acknowledgments
Part I. Generics
Chapter 1. Introduction
1.1  Generics
1.2  Boxing and Unboxing
1.3  Foreach
1.4  Generic Methods and Varargs
1.5  Assertions
Chapter 2. Subtyping and Wildcards
2.1  Subtyping and the Substitution Principle
2.2  Wildcards with extends
2.3  Wildcards with super
2.4  The Get and Put Principle
2.5  Arrays
2.6  Wildcards Versus Type Parameters
2.7  Wildcard Capture
2.8  Restrictions on Wildcards
Chapter 3. Comparison and Bounds
3.1  Comparable
3.2  Maximum of a Collection
3.3  A Fruity Example
3.4  Comparator
3.5  Enumerated Types
3.6  Multiple Bounds
3.7  Bridges
3.8  Covariant Overriding
Chapter 4. Declarations
4.1  Constructors
4.2  Static Members
4.3  Nested Classes
4.4  How Erasure Works
Chapter 5. Evolution, Not Revolution
5.1  Legacy Library with Legacy Client
5.2  Generic Library with Generic Client
5.3  Generic Library with Legacy Client
5.4  Legacy Library with Generic Client
5.4.1. Evolving a Library using Minimal Changes
5.4.2. Evolving a Library using Stubs
5.4.3. Evolving a Library using Wrappers
5.5  Conclusions
Chapter 6. Reification
6.1  Reifiable Types
6.2  Instance Tests and Casts
6.3  Exception Handling
6.4  Array Creation
6.5  The Principle of Truth in Advertising
6.6  The Principle of Indecent Exposure
6.7  How to Define ArrayList
6.8  Array Creation and Varargs
6.9  Arrays as a Deprecated Type?
6.10  Summing Up
Chapter 7. Reflection
7.1  Generics for Reflection
7.2  Reflected Types are Reifiable Types
7.3  Reflection for Primitive Types
7.4  A Generic Reflection Library
7.5  Reflection for Generics
7.6  Reflecting Generic Types
Chapter 8. Effective Generics
8.1  Take Care when Calling Legacy Code
8.2  Use Checked Collections to Enforce Security
8.3  Specialize to Create Reifiable Types
8.4  Maintain Binary Compatibility
Chapter 9. Design Patterns
9.1  Visitor
9.2  Interpreter
9.3  Function
9.4  Strategy
9.5  Subject-Observer
Part II. Collections
Chapter 10. The Main Interfaces of the Java Collections Framework
Chapter 11. Preliminaries
11.1  Iterable and Iterators
11.2  Implementations
11.3  Efficiency and the O-Notation
11.4  Contracts
11.5  Collections and Thread Safety
11.5.1. Synchronization and the Legacy Collections
11.5.2. JDK 1.2: Synchronized Collections and Fail-Fast Iterators
11.5.3. Concurrent Collections: Java 5 and Beyond
Chapter 12. The Collection Interface
12.1  Using the Methods of Collection
12.2  Implementing Collection
12.3  Collection Constructors
Chapter 13. Sets
13.1  Implementing Set
13.1.1. HashSet
13.1.2. LinkedHashSet
13.1.3. CopyOnWriteArraySet
13.1.4. EnumSet
13.2  SortedSet and NavigableSet
13.2.1. NavigableSet
13.2.2. TreeSet
13.2.3. ConcurrentSkipListSet
13.3  Comparing Set Implementations
Chapter 14. Queues
14.1  Using the Methods of Queue
14.2  Implementing Queue
14.2.1. PriorityQueue
14.2.2. ConcurrentLinkedQueue
14.3  BlockingQueue
14.3.1. Using the Methods of BlockingQueue
14.3.2. Implementing BlockingQueue
LinkedBlockingQueue
ArrayBlockingQueue
PriorityBlockingQueue
DelayQueue
SynchronousQueue
14.4  Deque
14.4.1. Implementing Deque
ArrayDeque
LinkedList
14.4.2. BlockingDeque
Implementing BlockingDeque
14.5  Comparing Queue Implementations
Chapter 15. Lists
15.1  Using the Methods of List
15.2  Implementing List
15.2.1. ArrayList
15.2.2. LinkedList
15.2.3. CopyOnWriteArrayList
15.3  Comparing List Implementations
Chapter 16. Maps
16.1  Using the Methods of Map
16.2  Implementing Map
16.2.1. HashMap
16.2.2. LinkedHashMap
16.2.3. WeakHashMap
16.2.4. IdentityHashMap
16.2.5. EnumMap
16.3  SortedMap and NavigableMap
16.3.1. NavigableMap
16.3.2. TreeMap
16.4  ConcurrentMap
16.4.1. ConcurrentHashMap
16.5  ConcurrentNavigableMap
16.5.1. ConcurrentSkipListMap
16.6  Comparing Map Implementations
Chapter 17. The Collections Class
17.1  Generic Algorithms
17.1.1. Changing the Order of List Elements
17.1.2. Changing the Contents of a List
17.1.3. Finding Extreme Values in a Collection
17.1.4. Finding Specific Values in a List
17.2  Collection Factories
17.3  Wrappers
17.3.1. Synchronized Collections
17.3.2. Unmodifiable Collections
17.3.3. Checked Collections
17.4  Other Methods
Index
Java Generics and Collections Maurice Naftalin and Philip Wadler Beijing•Cambridge•Farnham•Köln•Sebastopol•Taipei•Tokyo
Java Generics and Collections by Maurice Naftalin and Philip Wadler Copyright © 2007 O’Reilly Media . All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safari.oreilly.com). For more information, contact our corporate/ institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Mike Loukides Production Services: Windfall Software Indexers: Maurice Naftalin and Philip Wadler Cover Designer: Karen Montgomery Printing History: October 2006: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Java Generics and Collections, the image of an alligator, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. This book uses RepKover™, a durable and flexible lay-flat binding. ISBN: 978-0-596-52775-4 [M] 1233352047 [2/09] TM
We dedicate this book to Joyce Naftalin, Lionel Naftalin, Adam Wadler, and Leora Wadler —Maurice Naftalin and Philip Wadler
Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Part I. Generics 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 4 6 9 10 12 1.1 Generics 1.2 Boxing and Unboxing 1.3 Foreach 1.4 Generic Methods and Varargs 1.5 Assertions 2. Subtyping and Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 15 17 18 19 22 25 27 28 2.1 Subtyping and the Substitution Principle 2.2 Wildcards with extends 2.3 Wildcards with super 2.4 The Get and Put Principle 2.5 Arrays 2.6 Wildcards Versus Type Parameters 2.7 Wildcard Capture 2.8 Restrictions on Wildcards 3. Comparison and Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 31 34 36 37 42 45 47 49 3.1 Comparable 3.2 Maximum of a Collection 3.3 A Fruity Example 3.4 Comparator 3.5 Enumerated Types 3.6 Multiple Bounds 3.7 Bridges 3.8 Covariant Overriding v
4. Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 51 52 53 55 4.1 Constructors 4.2 Static Members 4.3 Nested Classes 4.4 How Erasure Works 5.1 Legacy Library with Legacy Client 5.2 Generic Library with Generic Client 5.3 Generic Library with Legacy Client 5.4 Legacy Library with Generic Client 5. Evolution, Not Revolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 60 60 62 65 65 68 68 71 5.4.1 Evolving a Library using Minimal Changes 5.4.2 Evolving a Library using Stubs 5.4.3 Evolving a Library using Wrappers 5.5 Conclusions 6. Reification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 73 74 79 80 82 86 89 90 92 95 6.1 Reifiable Types 6.2 Instance Tests and Casts 6.3 Exception Handling 6.4 Array Creation 6.5 The Principle of Truth in Advertising 6.6 The Principle of Indecent Exposure 6.7 How to Define ArrayList 6.8 Array Creation and Varargs 6.9 Arrays as a Deprecated Type? 6.10 Summing Up 7. Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 97 100 101 101 104 105 7.1 Generics for Reflection 7.2 Reflected Types are Reifiable Types 7.3 Reflection for Primitive Types 7.4 A Generic Reflection Library 7.5 Reflection for Generics 7.6 Reflecting Generic Types 8. Effective Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 109 111 112 117 8.1 Take Care when Calling Legacy Code 8.2 Use Checked Collections to Enforce Security 8.3 Specialize to Create Reifiable Types 8.4 Maintain Binary Compatibility vi | Table of Contents
分享到:
收藏