📄 tij0093.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0092.html">Prev</a> | <a href="tij0094.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
The
new collections
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
me, collection classes are one of the most powerful tools for raw programming.
You might have gathered that I’m somewhat disappointed in the collections
provided in Java through version 1.1. As a result, it’s a tremendous
pleasure to see that collections were given proper attention in <A NAME="Index828"></A><A NAME="Index829"></A><A NAME="Index830"></A><A NAME="Index831"></A>Java
1.2, and thoroughly redesigned (by Joshua Bloch at Sun). I consider the new
collections to be one of the two major features in Java 1.2 (the other is the
Swing library, covered in Chapter 13) because they significantly increase your
programming muscle and help bring Java in line with more mature programming
systems.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Some
of the redesign makes things tighter and more sensible. For example, many names
are shorter, cleaner, and easier to understand, as well as to type. Some names
are changed to conform to accepted terminology: a particular favorite of mine
is “iterator” instead of “enumeration.”
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
redesign also fills out the functionality of the collections library. You can
now have the behavior of <A NAME="Index832"></A>linked
lists, <A NAME="Index833"></A>queues,
and <A NAME="Index834"></A>dequeues
(double-ended queues, pronounced “decks”).
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
design of a collections library is difficult (true of most library design
problems). In <A NAME="Index835"></A><A NAME="Index836"></A>C++,
the STL covered the bases with many different classes. This was better than
what was available prior to the STL (nothing), but it didn’t translate
well into Java. The result was a rather confusing morass of classes. On the
other extreme, I’ve seen a collections library that consists of a single
class, “collection,” which acts like a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">at
the same time. The designers of the new collections library wanted to strike a
balance: the full functionality that you expect from a mature collections
library, but easier to learn and use than the STL and other similar collections
libraries. The result can seem a bit odd in places. Unlike some of the
decisions made in the early Java libraries, these oddities were not accidents,
but carefully considered decisions based on tradeoffs in complexity. It might
take you a little while to get comfortable with some aspects of the library,
but I think you’ll find yourself rapidly acquiring and using these new
tools.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
new collections library takes the issue of “holding your objects”
and divides it into two distinct concepts:
</FONT><P></DIV><DIV ALIGN=LEFT><A NAME="Index837"></A><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> </FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:
a group of individual elements, often with some rule applied to them. A <A NAME="Index838"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
must hold the elements in a particular sequence, and a <A NAME="Index839"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Set</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
cannot have any duplicate elements. (A <A NAME="Index840"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>bag</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which is not implemented in the new collections library since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
provide you with that functionality, has no such rules.)
</FONT><LI><A NAME="Index841"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> </FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:
a group of key-value object pairs (what you’ve seen up until now as a <A NAME="Index842"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
At first glance, this might seem like it ought to be a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
of pairs, but when you try to implement it that way the design gets awkward, so
it’s clearer to make it a separate concept. On the other hand, it’s
convenient to look at portions of a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
by creating a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to represent that portion. Thus, a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can return a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Set</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
of its keys, a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">of
its values, or a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">of
its pairs.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s,
like arrays, can easily be expanded to multiple dimensions without adding new
concepts: you simply make a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
whose values are
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
(and the values of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>those</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
can be
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s,
etc.).
</FONT></OL><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
may be implemented in many different ways, according to your programming needs.
It’s helpful to look at a diagram of the new collections:
</FONT><a name="Collections_Diagram"></a><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
diagram can be a bit overwhelming at first, but throughout the rest of this
chapter you’ll see that there are really only three collection components:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Set</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and only two or three implementations of each one
</FONT><A NAME="fnB37" HREF="#fn37">[37]</A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(with, typically, a preferred version). When you see this, the new collections
should not seem so daunting.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
dashed boxes represent
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s,
the dotted boxes represent
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>abstract</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
classes, and the solid boxes are regular (concrete) classes. The dashed arrows
indicate that a particular class is implementing an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(or in the case of an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>abstract</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class, partially implementing that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
The double-line arrows show that a class can produce objects of the class the
arrow is pointing to. For example, any
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can produce an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Iterator</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
while a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can produce a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ListIterator</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(as well as an ordinary
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Iterator</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is inherited from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
interfaces that are concerned with holding objects are
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Set</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Typically,
you’ll write the bulk of your code to talk to these interfaces, and the
only place where you’ll specify the precise type you’re using is at
the point of creation. So you can create a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
like this:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">List
x = new LinkedList();
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Of
course, you can also decide to make
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>x
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>LinkedList
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">(instead
of a generic
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
carry the precise type information around with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>x</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The beauty (and the intent) of using the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is that if you decide you want to change your implementation, all you need to
do is change it at the point of creation, like this:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">List
x = new ArrayList();
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
rest of your code can remain untouched.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
the class hierarchy, you can see a number of classes whose names begin with
“
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Abstract</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,”
and these can seem a bit confusing at first. They are simply tools that
partially implement a particular interface. If you were making your own
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Set</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
for example, you wouldn’t start with the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Set</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
interface and implement all the methods, instead you’d inherit from <A NAME="Index843"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>AbstractSet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and do the minimal necessary work to make your new class. However, the new
collections library contains enough functionality to satisfy your needs
virtually all the time. So for our purposes, you can ignore any class that
begins with “
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Abstract</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.”</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Therefore,
when you look at the diagram, you’re really concerned with only those
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
at the top of the diagram and the concrete classes (those with solid boxes
around them). You’ll typically make an object of a concrete class, upcast
it to the corresponding
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and then use the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
throughout the rest of your code. Here’s a simple example, which fills a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects and then prints each element in the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: SimpleCollection.java</font>
<font color="#009900">// A simple example using the new Collections</font>
<font color="#0000ff">package</font> c08.newcollections;
<font color="#0000ff">import</font> java.util.*;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -