【实例简介】
[Java泛型和集合].(Java.Generics.and.Collections).Maurice.Naftalin&Philip.Wadler.文字版.pdf
Java generics and collections
Maurice Naftalin and Philip Wadler
○ REILLY
Beijing· Cambridge: Farnham·Kdln· 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
arealsoavailableformosttitles(http://safari.oreillycom).Formoreinformationcontactourcorporate
institutionalsalesdepartment:(800)998-9938orcorporate@oreilly.com
Editor: Mike Loukides
Indexers: Maurice Naftalin and Philip Wadler
Production Services: Windfall Software
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
rademarks. 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
Repkover
This book uses RepkoverTM, a durable and flexible lay-flat binding
ISBN:978-0-596-52775-4
2/09
1233352047
We dedicate this book to Joyce Naftalin, Lionel
Naftalin, Adam wadler, and Leora wadler
-Maurice Naftalin and Philip wadler
Table of contents
Preface
X
Part. generics
1. Introduction
1.1 Generics
1.2 Boxing and Unboxing
1.3 Foreach
1.4 Generic Methods and varargs
10
1. 5 Assertions
2. Subtyping and Wildcards
15
2. 1 Subtyping and the Substitution Principle
15
2.2 Wildcards with extends
2.3 Wildcards with super
18
2. 4 The Get and Put Principle
19
2. 5 Arrays
22
2.6 Wildcards Versus Type Parameters
25
2.7 Wildcard Capture
27
2. 8 Restrictions on Wildcards
28
3. Comparison and bounds
31
3.1 Comparable
31
3.2 Maximum of a Collection
34
3.3 A Fruity Example
36
3. Comparator
3.5 Enumerated Types
42
3.6 Multiple bounds
3.7 bridges
3.8 Covariant Overriding
49
4. Declarations
51
4.1 Constructors
51
4.2 Static Members
52
4.3 Nested Classes
4.4 How Erasure works
5. Evolution not revolution
59
5.1 Legacy Library with Legacy Client
5.2 Generic Library with Generic Client
60
5.3 Generic Library with Legacy Client
62
5.4 Legacy Library with Generic Client
65
5.4.1 Evolving a Library using Minimal Changes
65
5.4.2 Evolving a Library using Stubs
5.4.3 Evolving a Library using Wrappers
5.5 Conclusions
6. Reification
73
6. 1 Reifiable Types
6.2 Instance Tests and Casts
74
6.3 Exception Handling
79
6. 4 Array Creation
80
6.5 The Principle of Truth in Advertising
6.6 The Principle of Indecent Exposure
86
6.7 How to Define array List
6.8 Array Creation and varargs
6.9 Arrays as a Deprecated Type?
92
6.10 Summing Up
95
7. Reflection
,97
7.1 Generics for Reflection
97
7.2 Reflected Types are Reifiable Types
100
7. 3 Reflection for Primitive l ypes
101
7.4 A Generic Reflection librar
101
7.5 Reflection for generics
104
7.6 Reflecting Generic Types
105
8. Effective generics .4...... 109
8.1 Take Care when Calling Legacy Code
109
8.2 Use Checked Collections to Enforce Security
8. 3 Specialize to Create Reifiable types
112
8.4 Maintain Binary Compatibility
117
ⅵ i Table of Contents
9. Design Patterns
,123
9.1 Visitor
123
9.2 Interpreter
127
9. 3 Function
128
9.4 Strategy
131
9.5 Subject-Observer
136
Part collections
10. The main Interfaces of the java collections framework
145
11. Preliminaries
147
11.1 Iterable and iterators
147
11.2 Implementations
149
11.3 Efficiency and the o-Notation
150
11. 4 Contracts
15
11.5 Collections and Thread Safety
153
11.5.1 Synchronization and the legacy collections
155
11.5.2 JDK 1. 2: Synchronized Collections and Fail-Fast Iterators 156
11.5.3 Concurrent Collections: Java 5 and Beyond
158
12. The collection
12. 1 Using the Methods of Collection
164
12.2 Implementing Collection
169
12. 3 Collection Constructors
169
13. Sets
171
13. 1 Implementing set
171
13.1.1 HashSet
13.1.2 LinkedHashSet
174
13.1.3 CopyOn WriteArraySet
175
13.1.4 EnumSet
176
13.2 SortedSet and NavigableSet
178
13.2.1 NavigableSet
181
13.2.2 TreeSet
184
13.2.3 ConcurrentSkipListSet
186
13.3 Comparing Set Implementations
188
14. Queues..
191
14.1 Using the Methods of Queue
193
14.2 Implementing Queue
195
14.2.1 Priority Queue
195
Table of contents|ⅶi
14.2.2 ConcurrentLinkedQueue
197
14.3 Blocking Queue
198
14.3.1 Using the Methods of BlockingQueue
199
14.3.2 Implementing Blocking Queue
202
14.4 Deque
206
14.4.1 Implementing Deque
208
14.4.2 Blocking Deque
209
14.5 Comparing Queue Implementations
210
15. lists
213
15. 1 Using the Methods of list
215
15.2 Implementing List
218
15.2.1 Arrayli
218
15.2.2 LinkedList
221
py
On writearray list
221
15.3 Comparing List Implementations
221
16. Maps
223
6.1 USing the Methods of Map
225
16.2Im
nting Map
226
16.2.1 HashMa
227
16.2.2 LinkedHashM
p
227
16.2.3 WeakHash Map
229
16.2.4 Identity HashMap
231
6.2.5 EnumMap
233
16.3 SortedMap and Navigable Map
234
16.3.1 NavigableMap
235
16.3.2 TreeMap
236
16.4 ConcurrentMap
237
16.4.1 ConcurrentHashMap
238
16.5 ConcurrentNavigable Map
238
16.5.1 ConcurrentSkipList Map
239
16.6 Comparing Map Implementations
239
17. The collections class
241
17.1 Generic Algorithms
241
17.1.1 Changing the Order of List elemer
241
17.1.2 Changing the Contents of a List
242
17. 1.3 Finding Extreme Values in a Collection
243
17. 1. Finding Specific Values in a List
243
17.2 Collection Factories
244
17.3 Wrappers
245
17.3.1 Synchronized Collections
245
ⅶ ii Table of Contents
【实例截图】
【核心代码】