Tài liệu Java Collections John Zukowski pptx

295 283 0
Tài liệu Java Collections John Zukowski pptx

Đ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

Java Collections John Zukowski Copyright © 2001 by John Zukowski All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN (pbk): 1-893115-92-5 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Editorial Directors: Dan Appleman, Gary Cornell, Karen Watterson Technical Editor: Kim Topley Developmental Editor and Copy Editor: Kiersten Burke Production Editor: Kari Brooks Compositor: Impressions Book and Journal Services, Inc. Indexer: Carol Burbo Cover Designer: Karl Miyajima Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 175 Fifth Avenue, New York, NY, 10010 and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. Acknowledgments Writing this book has been an interesting experience. So much has changed from when I started until now, it's almost hard to believe. No more "Focus on Java" at About.com, no more employment by jGuru (though I still consult for them), and I'm now off on my own with JZ Ventures, Inc. If you're in need of strategic Java consulting . As always, it's time to thank everyone who helped take my horrible writing and awful drawings into the book you're holding today. At Apress, I'd like to thank Gary Cornell for the book's concept, without which this may have ended up being just another book on JavaServer Pages, XML, or some other already-covered topic. I would especially like to thank Kiersten Burke for putting up with me, Grace Wong for her roll as mediator, as well as Kari Brooks and Stephanie Rodriguez for their help in shaping up what you are holding today. Special thanks to technical editor Kim Topley who not only straightened me out on some technical lapses, but also helped me to fill in some gaps. Any remaining technical inaccuracies are mine alone. For their continued encouragement along the way, I'd like to personally acknowledge my brother-in- law, Scott Pross, for joining the ranks of geekhood, even if it is with certification from that other software company; my cousin Rachel Goodell, the traveling nurse—it was fun having you in Boston for a change; and Ted Behr, my personal coach, for reminding me that there is more to life than Java. And, of course, all the readers and jGuru Collections FAQ contributors whose ideas, questions, and encouragement should make this edition much better. As always, I am grateful to my wife, Lisa, for her patience and support in jumpstarting JZ Ventures, and our playful three-year old pup, Jaeger, who thinks it's more important for him to be amused than me to be productive. Thanks to Mom and Dad, too, may they enjoy their computer. Table of Contents Chapter 1: Java Collections Framework: An Overview .1 What Is This Book About? 1 Is This Book for You? 2 How Is This Book Structured? 2 How Do I Read the Diagrams? .3 Part I: The Historical Collection Classes 4 Chapter List 4 Chapter 2: Arrays .5 Overview 5 Array Basics .5 Declaring and Creating Arrays 7 Arrays of Primitives 8 Arrays of Objects 9 Multidimensional Arrays 9 Initializing Arrays .11 Passing Array Arguments and Return Values 12 Copying and Cloning Arrays .13 Array Immutability 14 Array Assignments .14 Checking for Array Equality 15 Array Reflection .16 Character Arrays 19 Summary 20 Chapter 3: The Vector and Stack Classes 21 Overview 21 Vector Basics .21 Creating Vectors .23 Adding Elements .24 Printing Vectors 26 Removing Elements 26 Replacing Elements 28 Sizing Vectors .29 Vector Immutability 30 Vector Operations 30 Fetching Elements .30 Finding Elements 32 Copying and Cloning Vectors .34 Checking Vectors for Equality .37 Hashing Vectors 37 Serializing Vector .37 Maintaining Listener Lists with a Vector .38 Vector Variables and Constants .42 Variables Defined with Vector .42 Variables Defined with AbstractList 43 Stack Basics .43 Creating Stacks .44 Operating Stacks .44 i Table of Contents Chapter 3: The Vector and Stack Classes Stack Example 45 Summary 46 Chapter 4: The Enumeration Interface 47 Enumeration Basics .47 The SequenceInputStream Class 48 StringTokenizer 49 Creating Custom Enumerations .49 Summary 51 Chapter 5: The Dictionary, Hashtable, and Properties Classes .52 Overview 52 Dictionary Basics .52 Hashtable Basics 53 Understanding Hash Tables 54 Creating Hash Tables 56 Adding Key−Value Pairs 56 Displaying Hash Table Contents 57 Removing Key−Value Pairs .57 Sizing Hash Tables .57 Operating with Hash Tables .58 Fetching Keys and Values 58 Finding Elements 59 Cloning Hash Tables .60 Checking Hash Tables for Equality 60 Hashing Hash Tables 60 Serializing Hash Tables 60 Hashtable Immutability .60 Generating Hash Codes 61 Counting Word Occurrences 61 UIDefaults Demonstration 63 Properties Basics 64 Using Properties .65 Setting and Getting Elements .65 Getting a List 66 Loading and Saving 66 System Properties .67 Working with Security Providers .69 Understanding Resource Bundles 72 Summary 72 Chapter 6: The BitSet Class .73 Overview 73 BitSet Basics 73 Creating Bit Sets .74 Printing Bit Sets 74 Bit Set Operations 74 Manipulating Individual Bits 74 Manipulating Sets of Bits .75 ii Table of Contents Chapter 6: The BitSet Class Determining Set Size 76 Cloning Bit Sets 76 Checking Bit Sets for Equality .76 Hashing Bit Sets 76 Using BitSet: an Example 77 Summary 78 Part II: The Collections Framework .79 Chapter List 79 79 Chapter 7: Collections Introduction .80 Overview 80 Framework Basics 80 Framework Interfaces .80 Framework Implementations 81 Framework Algorithms .82 Collection Interface 82 Adding Elements .83 Removing Elements 84 Collection Operations 85 Fetching Elements .85 Finding Elements 86 Checking Size .86 Copying and Cloning Collections .86 Checking for Equality .87 Hashing Collections 88 Iterator Interface .88 Using an Iterator .88 Filtering Iterator 89 Collection Exceptions 91 ConcurrentModificationException .91 UnsupportedOperationException 92 Summary 92 Chapter 8: Sets 93 Overview 93 Set Basics .93 HashSet Class 94 Creating a HashSet 95 Adding Elements .95 Removing Elements 96 Set Operations 98 Fetching Elements .98 Finding Elements 99 Checking Size .99 Copying and Cloning Sets 99 Checking for Equality .101 Hashing Collections 101 TreeSet Class .101 iii Table of Contents Chapter 8: Sets Creating a TreeSet 102 Adding Elements .103 Comparing 103 Retrieving the Ends .104 Fetching Elements .104 Working with Subsets .104 Summary 106 Chapter 9: Lists .107 Overview 107 List Basics 107 What's New 108 Usage Issues .109 ArrayList Class 110 Creating an ArrayList .111 Adding Elements .111 Getting an Element .113 Removing Elements 113 List Operations .115 Fetching Elements .115 Finding Elements 115 Replacing Elements 116 Checking Size .117 Checking Capacity 117 Copying and Cloning Lists .117 Checking for Equality .118 Hashing Lists 118 LinkedList Class 119 Creating a LinkedList .120 Adding Elements .120 Retrieving the Ends .120 Removing Elements 120 LinkedList Example 121 ListIterator 123 Summary 125 Chapter 10: Maps .127 Overview 127 Map Basics .127 Map.Entry Interface .128 HashMap Class 130 Creating a HashMap .130 Adding Key−Value Pairs 131 Displaying Contents 131 Removing Key−Value Pairs .132 Sizing Hash Maps .132 Map Operations 133 Fetching Keys and Values 133 Finding Elements 133 iv Table of Contents Chapter 10: Maps Cloning Hash Map 134 Checking Hash Maps for Equality 134 Hashing Hash Maps 134 Serializing Hash Maps 134 WeakHashMap Class .135 Creating a WeakHashMap 135 Understanding Weak References 135 Using a WeakHashMap 136 WeakHashMap Example 137 TreeMap Class .141 Creating a TreeMap 142 Viewing Sub Maps 142 Working with End Points 143 Sharing Comparators 143 Map Usage .143 Summary 146 Chapter 11: Sorting 147 Comparable Basics .147 System−Defined Comparable Classes 147 Understanding Comparable 148 Using Comparable 148 Comparator Basics .150 Understanding Comparator .150 Using Comparator .151 SortedSet 152 Understanding SortedSet 153 Using TreeSet .155 SortedMap 155 Understanding SortedMap 156 Using TreeMap .157 Summary 157 Chapter 12: Special Collections Support 158 Overview 158 Prebuilt Collections 159 Empty Collections .159 Singleton Collections 159 Wrapped Collections 160 Read−Only Collections .160 Thread−Safe Collections 161 Sorting 162 Sorting Lists 162 Reversing Order 163 Searching 163 Binary Searching .163 Finding Extremes 165 Generic List Operations .166 Copying Lists 166 v Table of Contents Chapter 12: Special Collections Support Filling Lists .166 Multiple−Copy Collections 167 Reversing Lists .167 Shuffling Lists .168 Sorting Lists 168 Summary 169 Chapter 13: Array Algorithm Support .170 Overview 170 Filling Arrays .170 Checking Equality 172 Sorting Arrays 173 Primitive Arrays 173 Object Arrays 174 Searching Arrays 175 Primitive Arrays 175 Object Arrays 177 Summary 177 Chapter 14: Custom Implementations 178 Overview 178 AbstractCollection Class 178 Subclassing AbstractCollection 179 Implementing Optional Methods 179 AbstractSet Class .180 Creating a Custom Set 180 AbstractList Class 182 Subclassing AbstractList .183 Implementing Optional Methods 183 AbstractSequentialList Class .184 Subclassing AbstractSequentialList 184 Implementing Optional Methods 185 AbstractMap Class .185 Subclassing AbstractMap .186 Implementing Optional Methods 186 Creating a Custom Map 186 Summary 189 Chapter 15: Compatibility Issues 190 Converting from Historical to New Collections 190 Vectors and Hashtables .190 Arrays 190 Enumerations 190 Converting from New to Historical Collections 192 Vectors and Hashtables .192 Arrays 192 Enumerations 192 Working with JDK 1.1 .193 Comparing Objects with JDK 1.1 .194 vi [...]... deal with collections like the Generic Collection Library for Java (JGL) from ObjectSpace Aside from rolling their own libraries or reusing those created by others, the Collections Framework (beginning with what came with the early beta release of the Java 2 platform, version 1.2) introduced support into the core Java APIs for manipulating data collections This book describes how to use this Collections. .. teach you how to program in Java If you need to learn, there are many good books While I would like everyone to learn from my Mastering Java 2 book (Sybex, 1998), other good learning books are Core Java 2, Volume 1: Fundamentals (Prentice Hall, 2000); Beginning Java 2 (Wrox, 2000); The Java Tutorial (Addison−Wesley, 2000); Thinking in Java (Prentice Hall, 2000); and Learning Java (O'Reilly, 2000) Which... alternate frameworks available A JavaWorld survey back in the summer of 1998 asked readers whether Sun should scrap its Collections API in favor of the more robust JGL (the results are posted at JavaWorld, http://www.javaworld.com/jw−08−1998/jw−08−pollresults.html) At the time, the Java 2 release wasn't final yet, and as Figure 1−1 shows, most people wanted Sun to dump the Collections Framework in favor... consider getting Java Programming: from the Beginning (Norton, 2000) If you can program with Java but you don't understand interfaces yet, you may have some trouble getting through the material in this book So what does that leave? This book will teach you about the Java Collections Framework If you'd like to learn more than the single chapter's coverage of the Collections Framework found in most Java books,... Collections API Reference TreeMap Class 268 TreeSet Class 269 UnsupportedOperationException Class 269 Vector Class 269 WeakHashMap Class 271 Appendix B: Collections Resources .272 Collections Implementations .272 Collections FAQs and Tutorials 273 Mailing Lists and Newsgroups 273 Collections Related... table operations, we'll also dig into some of the less commonly discussed tasks, such as array reflection and optimization techniques The Java Collections Framework Part Two deals with the Java Collections Framework This framework was introduced with the release of the Java 2 Standard Edition, version 1.2 With minor changes, it remains relatively the same in the 1.3 release of the Standard Edition (and... Vector class, Java provides a Stack class for the familiar last−in, first−out data structure Figure 3−1 shows the current hierarchy for these two classes With the Java 2 platform, version 1.2 release, this structure changed considerably with the introduction of the Collections Framework Figure 3−2 shows the original, more simplified look of the class hierarchy from both Java 1.0 and Java 1.1 The 1.3... Structures If you were to look at the class syllabus, you'd likely see topics such as linked lists, queues, stacks, and binary trees, among many other data structures In Java, these structures are part of the Java Collections Framework Collections are typically used in a business environment for short−term management of information This information may have been retrieved from a database, acquired over... development tools like JBuilder or Forté for Java Of course, since the collections library is not meant to be visually programmed, any old text editor like emacs will do How Is This Book Structured? This book is broken down into three parts and three appendices The first part introduces the historical collection classes; the second part describes the Java Collections Framework; and in the third part... favor of licensing JGL Figure 1−1: The 1998 JavaWorld survey results While there hasn't been a new survey since, many people find the Collections Framework much easier to use In addition, since the framework is a core API, not a third−party library, you do not need to redistribute the library with your application Of course, even Sun doesn't use their own Collections Framework in things like the Swing . Java Collections John Zukowski Copyright © 2001 by John Zukowski All rights reserved. No part of this work. techniques. The Java Collections Framework Part Two deals with the Java Collections Framework. This framework was introduced with the release of the Java 2 Standard

Ngày đăng: 22/12/2013, 19:16

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan