📄 tij0093.html
字号:
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean
removeAll(Collection)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">*Removes
all the elements that are contained in the argument. Returns true if any
removals occurred.
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean
retainAll(Collection)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">*Retains
only elements that are contained in the argument (an “intersection”
from set theory). Returns true if any changes occurred.
</FONT><P></DIV>
</TD>
</TR>
</TABLE></DIV>
<DIV ALIGN=LEFT><P></DIV>
<DIV ALIGN=LEFT><TABLE BORDER>
<COLGROUP>
<COL width="126">
<COL width="216">
</COLGROUP>
<TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int
size( )
</B></FONT><P></DIV>
</TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Returns
the number of elements in the Collection.
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object[]
toArray( )
</B></FONT><P></DIV>
</TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Returns
an array containing all the elements in the Collection.
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<P></TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">*This
is an “optional” method, which means it might not be implemented by
a particular Collection. If not, that method throws an
UnsupportedOperationException. Exceptions will be covered in Chapter 9.
</FONT><P></DIV>
</TD>
</TR>
</TABLE></DIV>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
following example demonstrates all of these methods. Again, these work with
anything that inherits from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">;
an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ArrayList</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is used as a kind of “least-common denominator”:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: Collection1.java</font>
<font color="#009900">// Things you can do with all Collections</font>
<font color="#0000ff">package</font> c08.newcollections;
<font color="#0000ff">import</font> java.util.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> Collection1 {
<font color="#009900">// Fill with 'size' elements, start</font>
<font color="#009900">// counting at 'start':</font>
<font color="#0000ff">public</font> <font color="#0000ff">static</font> Collection
fill(Collection c, <font color="#0000ff">int</font> start, <font color="#0000ff">int</font> size) {
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = start; i < start + size; i++)
c.add(Integer.toString(i));
<font color="#0000ff">return</font> c;
}
<font color="#009900">// Default to a "start" of 0:</font>
<font color="#0000ff">public</font> <font color="#0000ff">static</font> Collection
fill(Collection c, <font color="#0000ff">int</font> size) {
<font color="#0000ff">return</font> fill(c, 0, size);
}
<font color="#009900">// Default to 10 elements:</font>
<font color="#0000ff">public</font> <font color="#0000ff">static</font> Collection fill(Collection c) {
<font color="#0000ff">return</font> fill(c, 0, 10);
}
<font color="#009900">// Create & upcast to Collection:</font>
<font color="#0000ff">public</font> <font color="#0000ff">static</font> Collection newCollection() {
<font color="#0000ff">return</font> fill(<font color="#0000ff">new</font> ArrayList());
<font color="#009900">// ArrayList is used for simplicity, but it's</font>
<font color="#009900">// only seen as a generic Collection </font>
<font color="#009900">// everywhere else in the program.</font>
}
<font color="#009900">// Fill a Collection with a range of values:</font>
<font color="#0000ff">public</font> <font color="#0000ff">static</font> Collection
newCollection(<font color="#0000ff">int</font> start, <font color="#0000ff">int</font> size) {
<font color="#0000ff">return</font> fill(<font color="#0000ff">new</font> ArrayList(), start, size);
}
<font color="#009900">// Moving through a List with an iterator:</font>
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> print(Collection c) {
<font color="#0000ff">for</font>(Iterator x = c.iterator(); x.hasNext();)
System.out.print(x.next() + " ");
System.out.println();
}
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
Collection c = newCollection();
c.add("ten");
c.add("eleven");
print(c);
<font color="#009900">// Find max and min elements; this means</font>
<font color="#009900">// different things depending on the way</font>
<font color="#009900">// the Comparable interface is implemented:</font>
System.out.println("Collections.max(c) = " +
Collections.max(c));
System.out.println("Collections.min(c) = " +
Collections.min(c));
<font color="#009900">// Add a Collection to another Collection</font>
c.addAll(newCollection());
print(c);
c.remove("3"); <font color="#009900">// Removes the first one</font>
print(c);
c.remove("3"); <font color="#009900">// Removes the second one</font>
print(c);
<font color="#009900">// Remove all components that are in the</font>
<font color="#009900">// argument collection:</font>
c.removeAll(newCollection());
print(c);
c.addAll(newCollection());
print(c);
<font color="#009900">// Is an element in this Collection?</font>
System.out.println(
"c.contains(\"4\") = " + c.contains("4"));
<font color="#009900">// Is a Collection in this Collection?</font>
System.out.println(
"c.containsAll(newCollection()) = " +
c.containsAll(newCollection()));
Collection c2 = newCollection(5, 3);
<font color="#009900">// Keep all the elements that are in both</font>
<font color="#009900">// c and c2 (an intersection of sets):</font>
c.retainAll(c2);
print(c);
<font color="#009900">// Throw away all the elements in c that</font>
<font color="#009900">// also appear in c2:</font>
c.removeAll(c2);
System.out.println("c.isEmpty() = " +
c.isEmpty());
c = newCollection();
print(c);
c.clear(); <font color="#009900">// Remove all elements</font>
System.out.println("after c.clear():");
print(c);
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first methods provide a way to fill any
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with test data, in this case just
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
converted to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s.
The second method will be used frequently throughout the rest of this chapter.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
two versions of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>newCollection( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
create
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ArrayList</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
containing different sets of data and return them as
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects, so it’s clear that nothing other than the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
interface is being used.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>print( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method will also be used throughout the rest of this section. Since it moves
through a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
using an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Iterator</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which 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, it will work with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Set</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
and any
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
produces.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
uses simple exercises to show all of the methods in
</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
following sections compare the various implementations of
</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">
and indicate in each case (with an asterisk) which one should be your default
choice. You’ll notice that the legacy classes
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -