1. Trang chủ
  2. » Công Nghệ Thông Tin

java generics and caollections

286 486 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 286
Dung lượng 3,92 MB

Nội dung

www.traintelco.com www.traintelco.com Java Generics and Collections Maurice Naftalin and Philip Wadler Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo Downloa d f r o m W o w ! e B o o k < w w w.woweb o o k . c o m > www.traintelco.com 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. TM This book uses RepKover™, a durable and flexible lay-flat binding. ISBN: 978-0-596-52775-4 [M] [2/09] 1233352047 www.traintelco.com We dedicate this book to Joyce Naftalin, Lionel Naftalin, Adam Wadler, and Leora Wadler —Maurice Naftalin and Philip Wadler www.traintelco.com www.traintelco.com Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Part I. Generics 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1 Generics 4 1.2 Boxing and Unboxing 6 1.3 Foreach 9 1.4 Generic Methods and Varargs 10 1.5 Assertions 12 2. Subtyping and Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.1 Subtyping and the Substitution Principle 15 2.2 Wildcards with extends 17 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.4 Comparator 37 3.5 Enumerated Types 42 3.6 Multiple Bounds 45 3.7 Bridges 47 3.8 Covariant Overriding 49 v www.traintelco.com 4. Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 4.1 Constructors 51 4.2 Static Members 52 4.3 Nested Classes 53 4.4 How Erasure Works 55 5. Evolution, Not Revolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 5.1 Legacy Library with Legacy Client 60 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 68 5.4.3 Evolving a Library using Wrappers 68 5.5 Conclusions 71 6. Reification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 6.1 Reifiable Types 73 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 82 6.6 The Principle of Indecent Exposure 86 6.7 How to Define ArrayList 89 6.8 Array Creation and Varargs 90 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 Types 101 7.4 A Generic Reflection Library 101 7.5 Reflection for Generics 104 7.6 Reflecting Generic Types 105 8. Effective Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 8.1 Take Care when Calling Legacy Code 109 8.2 Use Checked Collections to Enforce Security 111 8.3 Specialize to Create Reifiable Types 112 8.4 Maintain Binary Compatibility 117 vi | Table of Contents Downloa d f r o m W o w ! e B o o k < w w w.woweb o o k . c o m > www.traintelco.com 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 II. 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 152 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 Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 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 172 13.1.2 LinkedHashSet 174 13.1.3 CopyOnWriteArraySet 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 PriorityQueue 195 Table of Contents | vii www.traintelco.com 14.2.2 ConcurrentLinkedQueue 197 14.3 BlockingQueue 198 14.3.1 Using the Methods of BlockingQueue 199 14.3.2 Implementing BlockingQueue 202 14.4 Deque 206 14.4.1 Implementing Deque 208 14.4.2 BlockingDeque 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 ArrayList 218 15.2.2 LinkedList 221 15.2.3 CopyOnWriteArrayList 221 15.3 Comparing List Implementations 221 16. Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 16.1 Using the Methods of Map 225 16.2 Implementing Map 226 16.2.1 HashMap 227 16.2.2 LinkedHashMap 227 16.2.3 WeakHashMap 229 16.2.4 IdentityHashMap 231 16.2.5 EnumMap 233 16.3 SortedMap and NavigableMap 234 16.3.1 NavigableMap 235 16.3.2 TreeMap 236 16.4 ConcurrentMap 237 16.4.1 ConcurrentHashMap 238 16.5 ConcurrentNavigableMap 238 16.5.1 ConcurrentSkipListMap 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 Elements 241 17.1.2 Changing the Contents of a List 242 17.1.3 Finding Extreme Values in a Collection 243 17.1.4 Finding Specific Values in a List 243 17.2 Collection Factories 244 17.3 Wrappers 245 17.3.1 Synchronized Collections 245 viii | Table of Contents www.traintelco.com [...]... features of Java 5 and 6 as we present them Following common terminology, we refer to the successive versions of Java as 1.0 up to 1.4 and then 5 and 6 We say Java before generics to refer to Java 1.0 through 1.4, and Java with generics to refer to Java 5 and 6 The design of generics for Java is influenced by a number of previous proposals— notably, GJ, by Bracha, Odersky, Stoutamire, and Wadler;... omit standard imports Code that uses the Java Collection Framework or other utility classes should be preceded by the line: import java. util.*; • Sample interactive sessions, showing command-line input and corresponding output, are shown in constant-width font, with user-supplied input preceded by a percent sign: % javac g/Stack .java g/ArrayStack .java g/Stacks .java l/Client .java Note: Client .java uses... use wildcards and bounds; we xi www.traintelco.com describe techniques for evolving your code; we explain subtleties connected with casts and arrays; we treat advanced topics such as the interaction between generics and security, and how to maintain binary compatibility; and we update common design patterns to exploit generics Much has been written on generics, and their introduction into Java has sparked... wildcards to GJ, proposed by Igarashi and Viroli; and further development of wildcards, by Torgersen, Hansen, Ernst, von der Ahé, Bracha, and Gafter Design of generics was carried out under the Java Community Process by a team led by Bracha, and including Odersky, Thorup, and Wadler (as parts of JSR 14 and JSR 201) Odersky’s GJ compiler is the basis of Sun’s current javac compiler xii | Preface www.traintelco.com... exceptions, and arrays The fit between generics and arrays is the worst rough corner of the language, and we formulate two principles to help you work around the problems Chapter 7 explains new features that relate generics and reflection, including the newly generified type Class and additions to the Java library that support reflection of generic types Chapter 8 contains advice on how to use generics. .. first five chapters focus on the fundamentals of generics Chapter 1 gives an overview of generics and other new features in Java 5, including boxing, foreach loops, and functions with a variable number of arguments Chapter 2 reviews how subtyping works and explains how wildcards let you use subtyping in connection with generics Chapter 3 describes how generics work with the Comparable interface, which... said of computer scientists, “Instead of standing on each other’s shoulders, we stand on each other’s toes.”) Thanks to generics, code using collections is easier to read and the compiler will catch more type errors Further, collections provide excellent illustrations of the use of generics One might say that generics and collections were made for each other, and, indeed, ease of use of collections was... with a backslash: % javac -Xlint:unchecked g/Stack .java g/ArrayStack .java \ % g/Stacks .java l/Client .java l/Client .java: 4: warning: [unchecked] unchecked call to push(E) as a member of the raw type Stack for (int i = 0; i . of Java 5 and 6 as we present them. Following common terminology, we refer to the successive versions of Java as 1.0 up to 1.4 and then 5 and 6. We say Java before generics to refer to Java. through 1.4, and Java with generics to refer to Java 5 and 6. The design of generics for Java is influenced by a number of previous proposals— notably, GJ, by Bracha, Odersky, Stoutamire, and Wadler;. might say that generics and collections were made for each other, and, indeed, ease of use of collections was one of the main reasons for introducing generics in the first place. Java 5 and 6 not

Ngày đăng: 27/10/2014, 00:52

TỪ KHÓA LIÊN QUAN