📄 j-scjp-10-1.html
字号:
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="topmain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index10.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="topsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-scjp-11-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="topfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><img border="0" src="../i/xprevious.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-scjp-10-2.html"><img alt="下页" border="0" src="../i/next.gif" name="topnext"></a></TD>
</TR>
</TABLE>
<table bgcolor="ffffff" cellspacing="0" cellpadding="2" border="0" height="400" width="100%">
<tr valign="bottom">
<a name="navskip"></a><td height="25" colspan="4"><img alt="10.集合框架 " src="imagemaster/titlebar10.jpg" border="0" height="25" width="562"></td>
</tr>
<tr>
<td bgcolor="ffffff" width="15"> </td><td bgcolor="ffffff" width="12"> </td><td valign="top" align="left" bgcolor="ffffff" width="*">
<p>
<br>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="90%"><font size="4" face="Verdana, Arial, Helvetica"><b>集合和集合接口 </b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr> 第 1 页(共3 页)</nobr></font></td>
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>
<code>Collection </code>是用于将多个数据元素组织到一起并操纵它们的对象。集合可以动态增长和缩小,这是它们比数组强的地方。 <code>Collection </code> 提供了方法,以向集合添加对象、从集合删除对象、检查对象是否在集合中、从集合检索对象,以及在集合中迭代。有关集合框架的更多信息请参阅 <a href="j-scjp-11-2.html">参考资料 </a>。 </p>
<p>
<b>
<code>collection </code> 接口</b>
<br>
核心 <code>collection</code> 接口的继承层次如下所示: </p>
<p>
<b>图 2. 核心 collection 接口的继承层次 </b>
<br>
<img src="images/collection_interface.gif">
</p>
<p>
<code>Collection</code> 是根接口,从它扩展出 <code>Set</code> 和 <code>List</code> 接口。 <code>Map</code> 接口不是从 <code>Collection</code> 口扩展的。另外,不要将 <code>Collection</code>
<i>接口 </i> 与包含集合的静态工具方法的 <code>Collection</code>
<i>类 </i>相混淆。</p>
<p>让我们分析一下这些接口的基本行为。 <code>List</code> 接口表示有序集合,而 <code>Set</code> 不能包含重复的元素。 <code>Map</code> 接口将惟一键与值相匹配。 <code>SortedSet</code> 包含已排序的元素,而 <code>SortedMap</code> 以键的排序顺序排列映射。下面列出了实现了这些接口的类: </p>
<table border="1">
<tr>
<td width="75pt">
<b>Set</b>
</td><td width="125pt">HashSet</td><td width="125pt">TreeSet</td><td width="125pt">LinkedHashSet</td><td width="125pt"></td>
</tr>
<tr>
<td width="75pt">
<b>List</b>
</td><td width="125pt">Vector</td><td width="125pt">ArrayList</td><td width="125pt">LinkedList</td><td width="125pt"></td>
</tr>
<tr>
<td width="75pt">
<b>Map</b>
</td><td width="125pt">HashMap</td><td width="125pt">HashTable</td><td width="125pt">TreeMap</td><td width="125pt">LinkedHashMap</td>
</tr>
</table>
<p>
<b>
<code>Set</code> 类 </b>
<br>
这些实现了 <code>Set</code> 接口的类不允许有重复的元素。 </p>
<p>
<code>HashSet</code> 没有排序或者分类。这个类为像 <code>add</code> 和 <code>remove</code> 这样的基本操作提供恒定的时间性能。 <code>TreeSet</code> 以元素升序顺序排列元素,根据元素的自然顺序分类。 </p>
<p>
<code>LinkedHashSet</code> 是已排序的 <code>HashSet</code>,它以插入或最后访问的顺序给出元素。例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
LinkedHashSet linkSet = new LinkedHashSet();
linkSet.add("mango");
linkSet.add("apple");
linkSet.add("mango");
linkSet.add("banana");
Iterator i = linkSet.iterator();
while(i.hasNext())
System.out.print(i.next()); // Prints "mango apple banana"
</code>
</pre>
<p>
<b>
<code>List</code> 类 </b>
</p>
<p>
<code>List</code> 是已排序的集合,使得定位访问和搜索成为可能。 </p>
<p>实现 <code>List</code> 的类按索引位置排序。 <code>ArrayList</code> 允许进行快速的迭代和速度一致的定位访问。 <code>Vector</code> 类似于 <code>ArrayList</code>,只是慢一些,因为它是同步的。 <code>LinkedList</code> 允许在开始和结束的地方进行快速的插入和删除,它常用于实现堆栈和队列。例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
ArrayList list = new ArrayList();
list.add("mango");
list.add("apple");
list.add("mango");
list.add("banana");
Iterator i = list.iterator();
while(i.hasNext())
System.out.print(i.next()); // Prints "mango apple mango banana"
</code>
</pre>
<p>
<b>
<code>Map</code> 类 </b>
<br>
实现了 <code>Map</code> 接口的类将惟一键映射到特定的值上。 </p>
<p>
<code>HashMap</code> 类没有分类或者排序。它允许一个 null 键和多个 null 值。 </p>
<p>
<code>Hashtable</code> 类似于 <code>HashMap</code>,但是不允许 null 键和 null 值。它也比 <code>HashMap</code> 慢,因为它是同步的。J2SE 1.4 <code>LinkedHashMap</code> 类按插入和最后访问的顺序迭代,它允许一个 null 键和多个 null 值。 </p>
<p>
<code>TreeMap</code> 是一个按键升序排列的映射,根据键的类的自然顺序分类。 </p>
<p>
<i>
<b>
<code>java.lang.Object</code> 的 <code>equals()</code> 方法 </b>
</i>
<br>
如下所示, <code>equals()</code> 方法应该实现一个相等关系: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
public boolean equals(Object obj)
</code>
</pre>
<p>此外, <code>equals()</code> 方法是: </p>
<ul>
<li>反身的(Reflexive):对任何引用值 <code>x</code>, <code>x.equals(x)</code> 应该返回 <code>true</code>。</li>
<li>对称的(Symmetric):对于任何引用值 <code>x</code> 和 <code>y</code>,<i>只有当 </i>
<code>y.equals(x)</code> 返回 <code>true</code> 时,<code>x.equals(y)</code> 才会返回 <code>true</code>。</li>
<li>传递的(Transitive):对于任何引用值 <code>x</code>、 <code>y</code> 和 <code>z</code>,如果 <code>x.equals(y)</code> 返回 <code>true</code> 并且 <code>y.equals(z)</code> 返回 <code>true</code>,那么 <code>x.equals(z)</code> 应该返回 <code>true</code>。</li>
<li>一致的(Consistent):对于任何引用值 <code>x</code> 和 <code>y</code>,只要在对对象的 equal 比较中使用的信息没有修改,那么对 <code>x.equals(y)</code> 的多次调用会全部返回 <code>true</code> 或者全部返回 <code>false</code>。 </li>
</ul>
<p>对于所有非空引用值 <code>x</code>, <code>x.equals(null)</code>应该返回 <code>false</code>。</p>
<p>
<i>
<b>
<code>java.lang.Object</code> 的 <code>hashCode()</code> 方法 </b>
</i>
<br>
如下所示,<code>java.lang.Object</code> 类的 <code>hashCode()</code> 方法返回对象的散列码值。对象的散列码值给出一个整数,可以被像 <code>HashSet</code> 和 <code>Hashtable</code> 这样的集合类用来将其对象映射到内存中的储存段中。散列过程使集合的对象的存储和检索效率更高。 </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
public int hashCode()
</code>
</pre>
<p>如果两个对象用 <code>equals()</code> 方法判断为相等,那么它们的散列码必须是相同的。所以只要覆盖 <code>equals()</code> 方法,就应该实现 <code>hashCode()</code>。 </p>
<p>不过,反过来就不一定需要。两个不相等的对象也可以有相同的散列码。这是因为不一定总有可能保证惟一的散列码。不过,最好对不同的对象有不同的散列码以改进效率和性能。 </p>
<p>
<code>equals()</code> 方法使用的对象比较逻辑通常用到两个对象的实例变量。 <code>hashCode()</code> 实现也应该使用同样的实例变量。例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
public class Test
{
int i;
public boolean equals(Object o)
{
Test t = (Test)o;
if (t.i == this.i)
return true;
return false;
}
public int hashCode()
{
return i * 31;
}
}
</code>
</pre>
<p>在 Java 应用程序的执行期间对同一个对象多次调用时,只要在对对象进行相等性比较时使用的信息没有修改过,那么 <code>hashCode()</code> 方法必须一致地返回同样的整数。在计算散列码时使用随机数并不适合,因为在多次调用这个方法时它不会返回同样的散列码。null 元素的散列码被定义为零。 </p>
<p>在实现 <code>equals()</code> 和 <code>hashCode()</code> 方法时,不应当使用瞬时变量。 </p>
<br>
</font></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="bottommain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index10.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="bottomsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-scjp-11-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="bottomfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><img border="0" src="../i/xprevious.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-scjp-10-2.html"><img alt="下页" border="0" src="../i/next.gif" name="bottomnext"></a></TD>
</TR>
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
</TABLE>
<TABLE width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img alt="" height="1" width="1" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="bbg" height="21"> <a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/index.shtml">关于 IBM</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/legal/index.shtml">法律条款</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/contact/index.shtml">联系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
<script src="//www.ibm.com/common/stats/stats.js" language="JavaScript1.2" type="text/javascript"></script>
<noscript>
<img border="0" alt="" height="1" width="1" src="//stats.www.ibm.com/rc/images/uc.GIF?R=noscript"></noscript>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -