Tải bản đầy đủ (.pdf) (4 trang)

Java Collections Framework An - Overview

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (16.42 KB, 4 trang )

Chapter 1: Java Collections Framework: An Overview
When learning a new computer programming language, one of the first things you tend to ask is how to work
with large groups of data. This topic is often covered in the second course of the standard curriculum for
Computer Science as part of a class typically called Data 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 the network, or just entered by a user.
Their primary purpose though is to help the data persist over the lifetime of a program, in a structure that is
efficient for the data's purpose. Different collections offer different purposes with quick insertions and
deletions but slower fetches, or the reverse, or something in between. Your typical computer science text
describes this information in what is called Big O notation and states how fast or slow operations take for each
data structure.
What Is This Book About?
This book is about Java's support for dealing with groups of data. Prior to the Java 2 release, the only standard
support for data structures and algorithms was a few fairly basic options available through arrays, hash tables,
and vectors. Many people either created their own standard data structure library or reused one of several
libraries introduced to 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 Framework. We'll also look at some of the common 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,
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 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 component set yet, whereas all the internal data models are still using the historical collection
classes. And for what its worth, ObjectSpace hasn't released a new version in close to two years. As of this
writing in early 2001, they just recently removed a reference from their Web site to the 1.2 release of Java
1
being in limited beta testing, months after the release of 1.3.
Note Given the name of the JGL library, you might think the acronym was coined when someone
spent too much time at the pub. It actually comes from an earlier name for the library that
violated Sun's Java trademarks. After a friendly letter from the legal department at Sun, the
product name was changed but the acronym remained. Most of the time you only see the
acronym with no mention of its illogical expansion.
Is This Book for You?
This book is not meant to be a textbook for a second course in computer science. It does not cover the material
in ACM's Curriculum '78, its revised version in '84, or even in the combined ACM/IEEE Computing
Curricula 1991! In fact, I've never read any of those.
This book will not 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 one is right for you depends upon your specific back−ground. If you are truly new to
programming, 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, this
book will provide you with a great deal more. In addition, this book describes several of the alternate
collection libraries, such as the JGL Libraries from ObjectSpace; Doug Lea's util.concurrent package; and the
Colt Distribution for high performance computing. With each of these other libraries, we'll take a quick look
at what they offer and I'll introduce you to the collections−related support available from each.
While this book isn't meant to be a computer science curriculum text, I do describe concepts like the details of
balanced trees and hash tables. An understanding of how each concept works helps you to decide which

collection implementation class is appropriate.
You will, of course, need a Java development environment. The latest JDK from Sun is sufficient, although
you might find yourself more productive with one of the visual 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 you'll find
descriptions of the alternate collection libraries. In most cases, you don't need to read the book from start to
finish; you can just go directly to the particular data structure, class, or interface you wish to learn more about.
The exception would be cases such as working with hash tables. Instead of describing them in multiple
sections, with both Hashtable and HashMap you'll find the deepest description in the first Hashtable chapter
and less in the HashMap chapter. Feel free to flip back and forth as necessary.
Is This Book for You?
2
The Historical Collection Classes
Part One of the book provides you with everything you ever wanted to know about the historical collection
classes. While most people know how to do most of the basic array, vector, and hash 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 is the same in the 1.2 release of the Enterprise Edition). The framework provides
enhanced support to manipulate groups of data and allows you to easily separate storage from access. We also
explore using the framework with the earlier 1.1 release of the JDK.
Alternative Collection Libraries
In Part Three, we'll discuss some of the other collection libraries out there. While the JGL Libraries seemed
like a de facto collection library years ago, its usage seems to be slipping of late. We'll introduce it here, along
with Doug's library and Colt.
Note Originating from work by Alexander Stephanov at Hewlett−Packard in 1979, the C++ Standard

Template Library, or STL as it is commonly known, is a standard part of the ANSI (American), ISO
(International), BSI (British), and DIN (German) adopted C++ standard of November 14, 1997. While
this book doesn't cover the STL, if you are interested in learning more about it, you can read "Mumit's
STL Newbie's Guide" available at />Keep in mind that the document is for the C++ developer and not the Java developer.
The Appendices
There are three appendices in this book. Appendix A provides an API reference to the core Java classes of the
Collections Framework. You'll find both a class−level API reference as well as an alphabetical class, method,
and field reference. In Appendix B you'll find a list of resources to help you become more productive with the
book's contents. Appendix C describes support for parameterized types, a prelude to something that could
become a Java standard soon, possibly as early as the 1.4 release.
The Source Code
This book does not include a CD. However, the source code from the book is freely available from the Apress
Web site at With nearly universal Internet access, at least for those programming
with Java, I hope that this will not be an issue for anyone.
How Do I Read the Diagrams?
In all three parts of the book, I've used UML to diagram the design of the classes and processes. If you're
already familiar with UML, you should have no problem understanding the diagrams. If you're not yet
familiar with the UML, consider reviewing The Unified Modeling Language User Guide (Addison−Wesley,
1998) or UML Distilled (Addison−Wesley, 1999) for a complete tutorial on the subject.
In the next chapter, you'll take a long look at array support in Java. Beginning with basic access and creation,
you'll quickly move into multi−dimensional array support, initialization, cloning, and immutability.
How Is This Book Structured?
3
Part I: The Historical Collection Classes
Chapter List
Chapter 2: Arrays
Chapter 3: The Vector and Stack Classes
Chapter 4: The Enumeration Interface
Chapter 5: The Dictionary, Hashtable, and Properties Classes
Chapter 6: The BitSet Class

4

×