Naftalin M., Wadler Ph.
Java Generics and Collections covers everything from the most basic uses of generics to the strangest corner cases. It teaches you everything you need to know about the collections libraries, so you’ll always know which collection is appropriate for any given task, and how 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 and generic 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 a new level. If you want to take your software development practice to a new level, this book is essential reading.
Philip Wadler is Professor of Theoretical Computer Science at the University of Edinburgh, where his research focuses on the design of programming languages. He is a co-designer of GJ, work that became 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 the topic, it provides a crystal clear tutorial that starts with the basics and ends leaving the reader with a deep understanding of both the use and design of generics.” Gilad Bracha, Java Generics Lead, Sun Microsystems
Table of contents :
I Introduction to Generics……Page 7
1 Getting started……Page 11
1.1 Generics……Page 12
1.2 Boxing and unboxing……Page 14
1.3 Foreach……Page 16
1.4 Generic methods and varargs……Page 17
1.6 Summing up……Page 19
2.1 Subtyping and The Substitution Principle……Page 21
2.2 Wildcards with extends……Page 22
2.3 Wildcards with super……Page 23
2.4 The Get and Put Principle……Page 24
2.5 Arrays……Page 27
2.6 Wildcards vs. type parameters……Page 29
2.7 Wildcard capture……Page 31
2.8 Restrictions on wildcards……Page 32
3.1 Comparable……Page 35
3.2 Maximum of a collection……Page 37
3.3 A fruity example……Page 39
3.4 Comparator……Page 42
3.5 Enumerated types……Page 45
3.6 Multiple bounds……Page 46
3.7 Bridges……Page 48
3.8 Static members……Page 49
3.9 Nested classes……Page 51
3.10 How erasure works……Page 52
4.1 Legacy library with legacy client……Page 57
4.3 Generic library with legacy client……Page 58
4.5 Evolving a library — minimal changes……Page 62
4.6 Evolving a library — stubs……Page 63
4.7 Conclusions……Page 66
5.1 Reifiable types……Page 67
5.2 Instance tests and casts……Page 68
5.3 Exception handling……Page 72
5.4 Array creation……Page 75
5.5 The Principle of Truth in Advertising……Page 78
5.6 The Principle of Indecent Exposure……Page 81
5.7 How to define ArrayList……Page 83
5.8 Array creation and varargs……Page 85
5.9 Summing up……Page 87
6.1 Generics for reflection……Page 89
6.2 Reflected types are reifiable types……Page 91
6.3 Reflection for primitive types……Page 92
6.4 A generic reflection library……Page 93
6.5 Reflection for generics……Page 94
6.6 Reflecting generic types……Page 95
II Collections……Page 103
7 The Main Interfaces of the Java Collections Framework……Page 107
8.1 Iterable and Iterators……Page 109
8.2 Comparable and Comparator……Page 111
8.3 Implementations……Page 112
8.4 Efficiency and the O-notation……Page 113
8.5 Contracts……Page 115
8.6 Collections and Thread-Safety……Page 116
8.6.1 Thread Safety and Iterators……Page 117
9 The Collection Interface……Page 119
9.1 Using the Methods of Collection……Page 121
9.2 Implementing Collection……Page 125
9.2.1 AbstractCollection subclasses……Page 128
10.1 Implementing Set……Page 131
10.1.1 HashSet……Page 134
10.1.3 LinkedHashSet……Page 136
10.2 SortedSet……Page 137
10.2.1 TreeSet……Page 139
10.3 Comparison of Set Implementations……Page 141
11 Queues……Page 143
11.1 Using the Methods of Queue……Page 144
11.2.1 LinkedList……Page 145
11.2.3 PriorityQueue……Page 147
11.3 BlockingQueue……Page 148
11.3.1 Implementing BlockingQueue……Page 150
12 Lists……Page 155
12.1 Using the Methods of List……Page 156
12.2 Implementing List……Page 159
12.2.1 AbstractList……Page 160
12.2.2 ArrayList……Page 163
12.2.4 LinkedList……Page 165
12.3 Comparison of List Implementations……Page 166
13 Maps……Page 169
13.1 Using the Methods of Map……Page 170
13.2 Implementing Map……Page 171
13.2.1 AbstractMap……Page 172
13.2.3 LinkedHashMap……Page 173
13.2.4 WeakHashMap……Page 174
13.2.5 IdentityHashMap……Page 175
13.3 SortedMap……Page 176
13.4.1 ConcurrentHashMap……Page 178
13.5 Comparison of Map Implementations……Page 179
14.1 Pre-populated Collections……Page 181
14.2.2 Synchronized Collections……Page 182
14.2.3 Checked Collections……Page 183
15.1 Changing the Order of List Elements……Page 185
15.3 Finding Specific Values in a List……Page 186
15.4 Changing the Contents of a List……Page 187
III Using Generics Effectively……Page 189
16.1 Avoid Unchecked Warnings……Page 191
16.2 Make Careful Use of @SuppressWarnings……Page 193
16.3 Favour Collections Over Arrays……Page 194
16.4 Favour Wildcards over Type Parameters for Generic Methods……Page 195
16.5 Use Generics for Typesafe Covariant Hierarchies……Page 197
16.6 Don’t Use Generics to Enforce Immutability……Page 201
17.1 Use Checked Collections……Page 203
17.2 Provide Wrappers to Legacy Code……Page 205
17.3 Choose Appropriate Types for Generification……Page 208
17.4 Maintain Binary Compatibility when Generifying Existing Classes……Page 209
Reviews
There are no reviews yet.