﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-asklxf-文章分类-Java Basic</title><link>http://www.blogjava.net/asklxf/category/5513.html</link><description>A java developer's notebook.</description><language>zh-cn</language><lastBuildDate>Fri, 02 Mar 2007 02:54:23 GMT</lastBuildDate><pubDate>Fri, 02 Mar 2007 02:54:23 GMT</pubDate><ttl>60</ttl><item><title>JDK源码分析：java.lang.Boolean</title><link>http://www.blogjava.net/asklxf/articles/22201.html</link><dc:creator>Xuefeng's Weblog</dc:creator><author>Xuefeng's Weblog</author><pubDate>Fri, 02 Dec 2005 02:35:00 GMT</pubDate><guid>http://www.blogjava.net/asklxf/articles/22201.html</guid><wfw:comment>http://www.blogjava.net/asklxf/comments/22201.html</wfw:comment><comments>http://www.blogjava.net/asklxf/articles/22201.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/asklxf/comments/commentRss/22201.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/asklxf/services/trackbacks/22201.html</trackback:ping><description><![CDATA[<P dir=ltr style="MARGIN-RIGHT: 0px">闲来无事，开始研究JDK源码（jdk 1.5 b2），先找了一个最简单的java.lang.Boolean开始解剖。<BR><BR>由于水平有限，难免有不少错误，还请大家指正！<BR><BR>首先我们剔除所有的方法和静态变量，Boolean的核心代码如下：</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P dir=ltr style="MARGIN-RIGHT: 0px"><FONT face="Courier New" color=#006400 size=2>public final class Boolean implements java.io.Serializable,Comparable<BR>{<BR>&nbsp;&nbsp;&nbsp; private final boolean value;<BR>}</FONT></P></BLOCKQUOTE>
<P dir=ltr style="MARGIN-RIGHT: 0px">很明显，凡是成员变量都是final类型的，一定是immutable class，这个Boolean和String一样，一旦构造函数执行完毕，实例的状态就不能再改变了。<BR><BR>Boolean的构造函数有两个：</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P dir=ltr style="MARGIN-RIGHT: 0px"><FONT face="Courier New" color=#006400 size=2>public Boolean(boolean value) {<BR>&nbsp;&nbsp;&nbsp; this.value = value;<BR>}<BR>public Boolean(String s) {<BR>&nbsp;&nbsp;&nbsp; this(toBoolean(s));<BR>}</FONT></P></BLOCKQUOTE>
<P dir=ltr style="MARGIN-RIGHT: 0px">都很简单就不多说了。<BR><BR>另外注意到Boolean类实际上只有两种不同状态的实例：一个包装true，一个包装false，Boolean又是immutable class，所以在内存中相同状态的Boolean实例完全可以共享，不必用new创建很多实例。因此Boolean class还提供两个静态变量：</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P dir=ltr style="MARGIN-RIGHT: 0px"><FONT face="Courier New" color=#006400 size=2>public static final Boolean TRUE = new Boolean(true);<BR>public static final Boolean FALSE = new Boolean(false);</FONT></P></BLOCKQUOTE>
<P dir=ltr style="MARGIN-RIGHT: 0px">这两个变量在Class Loader装载时就被实例化，并且申明为final，不能再指向其他实例。<BR><BR>提供这两个静态变量是为了让开发者直接使用这两个变量而不是每次都new一个Boolean，这样既节省内存又避免了创建一个新实例的时间开销。<BR><BR>因此，用<BR><FONT face="Courier New" color=#006400 size=2>&nbsp;&nbsp;&nbsp; Boolean b = Boolean.TRUE;<BR></FONT>比<BR><FONT face="Courier New" color=#006400 size=2>&nbsp;&nbsp;&nbsp; Boolean b = new Boolean(true);</FONT><BR>要好得多。<BR><BR>如果遇到下面的情况：<BR><FONT face="Courier New" color=#006400 size=2>&nbsp;&nbsp;&nbsp; Boolean b = new Boolean(var);</FONT><BR>一定要根据一个boolean变量来创建Boolean实例怎么办？<BR><BR>推荐你使用Boolean提供的静态工厂方法：<BR><FONT face="Courier New" color=#006400 size=2>&nbsp;&nbsp;&nbsp; Boolean b = Boolean.valueOf(var);</FONT><BR>这样就可以避免创建新的实例，不信看看valueOf()静态方法：</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P dir=ltr style="MARGIN-RIGHT: 0px"><FONT face="Courier New" color=#006400 size=2>public static Boolean valueOf(boolean b) {<BR>&nbsp;&nbsp;&nbsp; return (b ? TRUE : FALSE);<BR>}</FONT></P></BLOCKQUOTE>
<P dir=ltr style="MARGIN-RIGHT: 0px">这个静态工厂方法返回的仍然是两个静态变量TRUE和FALSE之一，而不是new一个Boolean出来。虽然Boolean非常简单，占用的内存也很少，但是一个复杂的类用new创建实例的开销可能非常大，而且，使用工厂方法可以方便的实现缓存实例，这对客户端是透明的。所以，能用工厂方法就不要用new。<BR><BR>和Boolean只有两种状态不同，Integer也是immutable class，但是状态上亿种，不可能用静态实例缓存所有状态。不过，SUN的工程师还是作了一点优化，Integer类缓存了-128到127这256个状态的Integer，如果使用Integer.valueOf(int i)，传入的int范围正好在此内，就返回静态实例。<BR><BR>hashCode()方法很奇怪，两种Boolean的hash code分别是1231和1237。估计写Boolean.java的人对这两个数字有特别偏好：</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P dir=ltr style="MARGIN-RIGHT: 0px"><FONT face="Courier New" color=#006400 size=2>public int hashCode() {<BR>&nbsp;&nbsp;&nbsp; return value ? 1231 : 1237;<BR>}</FONT></P></BLOCKQUOTE>
<P dir=ltr style="MARGIN-RIGHT: 0px">equals()方法也很简单，只有Boolean类型的Object并且value相等才返true：</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P dir=ltr style="MARGIN-RIGHT: 0px"><FONT face="Courier New" color=#006400 size=2>public boolean equals(Object obj) {<BR>&nbsp;&nbsp;&nbsp; if (obj instanceof Boolean) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return value == ((Boolean)obj).booleanValue();<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; return false;<BR>}</FONT></P></BLOCKQUOTE>
<P dir=ltr style="MARGIN-RIGHT: 0px">顺便提一句：很多人写equals()总是在第一行写：<BR><FONT face="Courier New">&nbsp;&nbsp; <FONT color=#006400 size=2>if (obj==null) return false;</FONT></FONT><BR>其实完全没有必要，因为如果obj==null，下一行的<BR><FONT face="Courier New" color=#006400 size=2>&nbsp;&nbsp;&nbsp; if (obj instanceof Type)</FONT><BR>就肯定返回false，因为(null instanceof AnyType) = false。<BR>详细内容请参考《Effective Java》第7条：Obey the general contract when overriding equals。<BR><BR>其他的方法如toString()就更简单了，只要稍微熟悉java的程序员相信都能写出来，我就不多说了。<BR><BR><FONT color=#ff0000>★</FONT> 总结 <FONT color=#ff0000>★</FONT><BR><BR>1.如果一个类只有有限的几种状态，考虑用几个final的静态变量来表示不同状态的实例。<BR>例如编写一个Weekday类，状态只有7个，就不要让用户写new Weekday(1)，直接提供Weekday.MONDAY即可。<BR><BR>2.要防止用户使用new生成实例，就取消public构造函数，用户要获得静态实例的引用有两个方法：如果申明public static var就可以直接访问，比如Boolean.TRUE，<BR>第二个方法是通过静态工厂方法：<FONT style="BACKGROUND-COLOR: #ffffff">Boolean.valueOf(?)</FONT><BR><BR>3.如果不提供public构造函数，让用户只能通过上面的方法获得静态变量的引用，还可以大大简化equals()方法：<BR><FONT face="Courier New" color=#006400 size=2>&nbsp;&nbsp;&nbsp; public boolean equals(Object obj) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this==obj;<BR>&nbsp;&nbsp;&nbsp; }<BR></FONT>可以直接用==比较引用，绝对没有问题，而且效率最高。<BR><BR>4.为什么JDK的Boolean没有实现上面第3点？因为那两个static变量TRUE和FALSE是在jdk 1.2以后才有的，由于前面的版本已经把构造函数申明为public，所以为了保持客户端代码能够不修改也在后面的版本中运行，只好继续提供public构造函数。</P><img src ="http://www.blogjava.net/asklxf/aggbug/22201.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/asklxf/" target="_blank">Xuefeng's Weblog</a> 2005-12-02 10:35 <a href="http://www.blogjava.net/asklxf/articles/22201.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>RMI调用模版</title><link>http://www.blogjava.net/asklxf/articles/22200.html</link><dc:creator>Xuefeng's Weblog</dc:creator><author>Xuefeng's Weblog</author><pubDate>Fri, 02 Dec 2005 02:34:00 GMT</pubDate><guid>http://www.blogjava.net/asklxf/articles/22200.html</guid><wfw:comment>http://www.blogjava.net/asklxf/comments/22200.html</wfw:comment><comments>http://www.blogjava.net/asklxf/articles/22200.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/asklxf/comments/commentRss/22200.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/asklxf/services/trackbacks/22200.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto">1. </SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="BACKGROUND: #d9d9d9; FONT-FAMILY: 宋体; mso-ascii-font-family: Tahoma; mso-hansi-font-family: Tahoma; mso-bidi-font-family: Tahoma; mso-shading: white; mso-pattern: gray-15 auto">定义远程接口</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">远程接口继承自</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">Remote<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">远程方法的传入参数和返回值必须是自然类型（</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">int</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">，</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">float</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">，</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">boolean</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">等）</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">或者实现了</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">Serializable</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">或</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">Remote</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">接口的对象。</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">public</SPAN></B><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> <B style="mso-bidi-font-weight: normal">interface</B> Time <B style="mso-bidi-font-weight: normal">extends</B> java.rmi.Remote {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">远程方法必须抛出</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">RemoteException</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">：</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">public</B> String getTime() <B style="mso-bidi-font-weight: normal">throws</B> RemoteException;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto"><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto"></SPAN></B>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto">2. </SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="BACKGROUND: #d9d9d9; FONT-FAMILY: 宋体; mso-ascii-font-family: Tahoma; mso-hansi-font-family: Tahoma; mso-bidi-font-family: Tahoma; mso-shading: white; mso-pattern: gray-15 auto">定义实现类</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">注意：实现类继承自</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">UnicastRemoteObject</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">和自定义的远程接口</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">Time</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">：</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">public</SPAN></B><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> <B style="mso-bidi-font-weight: normal">class</B> TimeImpl <B style="mso-bidi-font-weight: normal">extends</B> java.rmi.server.UnicastRemoteObject <B style="mso-bidi-font-weight: normal">implements</B> Time {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">注意：由于</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">RemoteObject</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">构造函数要抛出</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">RemoteException</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">，</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">因此务必定义构造函数并抛出</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">RemoteException</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">：</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">public</B> TimeImpl() <B style="mso-bidi-font-weight: normal">throws</B> RemoteException { <B style="mso-bidi-font-weight: normal">super</B>(); }<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">这里是远程方法：</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">public</B> String getTime() <B style="mso-bidi-font-weight: normal">throws</B> RemoteException {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">return</B> "<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:time w:st="on" Minute="4" Hour="12">12:04:27</st1:time>";<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">启动服务：</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">public</B> <B style="mso-bidi-font-weight: normal">static</B> <B style="mso-bidi-font-weight: normal">void</B> main(String[] args) <B style="mso-bidi-font-weight: normal">throws</B> Exception {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">可以手动启动</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">RMI Registry</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">，也可以在程序中启动：</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>java.rmi.registry.LocateRegistry.createRegistry(1099);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">绑定名字服务，地址是本地计算机名或本机</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">IP</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">，默认端口是</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">1099</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">：</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>java.rmi.Naming.bind("//localhost:1099/servicename", <B style="mso-bidi-font-weight: normal">new</B> TimeImpl());<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">如果没有异常抛出，则绑定成功。</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">如果名字已经被绑定，可以用</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">Naming.rebind()</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">替换掉已绑定的服务。</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="FONT-SIZE: 9pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 'Courier New'; mso-shading: white; mso-pattern: gray-15 auto"><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto"></SPAN></B>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto">3. </SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="BACKGROUND: #d9d9d9; FONT-FAMILY: 宋体; mso-ascii-font-family: Tahoma; mso-hansi-font-family: Tahoma; mso-bidi-font-family: Tahoma; mso-shading: white; mso-pattern: gray-15 auto">编译生成桩和框架</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">运行</SPAN><I style="mso-bidi-font-style: normal"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">rmic TimeImpl</SPAN></I><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">，生成</SPAN><I style="mso-bidi-font-style: normal"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">TimeImpl_Skel.class</SPAN></I><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">和</SPAN><I style="mso-bidi-font-style: normal"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">TimeImpl_Stub.class</SPAN></I><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">。</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto"><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto"></SPAN></B>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto">4. </SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="BACKGROUND: #d9d9d9; FONT-FAMILY: 宋体; mso-ascii-font-family: Tahoma; mso-hansi-font-family: Tahoma; mso-bidi-font-family: Tahoma; mso-shading: white; mso-pattern: gray-15 auto">客户端</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="BACKGROUND: #d9d9d9; FONT-FAMILY: Tahoma; mso-shading: white; mso-pattern: gray-15 auto"> </SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">客户端文件包含客户端代码</SPAN><I style="mso-bidi-font-style: normal"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">Client.class</SPAN></I><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">，远程接口</SPAN><I style="mso-bidi-font-style: normal"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">Time.class</SPAN></I><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">，</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">由</SPAN><I style="mso-bidi-font-style: normal"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">rmic</SPAN></I><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">生成的支持类</SPAN><I style="mso-bidi-font-style: normal"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">TimeImpl_Skel.class</SPAN></I><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">和</SPAN><I style="mso-bidi-font-style: normal"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">TimeImpl_Stub.class</SPAN></I><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">：</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">public</SPAN></B><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> <B style="mso-bidi-font-weight: normal">static</B> <B style="mso-bidi-font-weight: normal">void</B> main(String[] args) <B style="mso-bidi-font-weight: normal">throws</B> Exception {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">客户端通过</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">IP</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">引用服务器端的远程对象，因此可以动态选择服务器。</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>// </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">如果不指定端口，默认端口号是</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">1099</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">：</SPAN><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>Time time = (Time)java.rmi.Naming.lookup("//localhost:1099/servicename");<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>System.out.println(time.getTime());<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">}<o:p></o:p></SPAN></P><img src ="http://www.blogjava.net/asklxf/aggbug/22200.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/asklxf/" target="_blank">Xuefeng's Weblog</a> 2005-12-02 10:34 <a href="http://www.blogjava.net/asklxf/articles/22200.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>细说Java之util类</title><link>http://www.blogjava.net/asklxf/articles/22195.html</link><dc:creator>Xuefeng's Weblog</dc:creator><author>Xuefeng's Weblog</author><pubDate>Fri, 02 Dec 2005 02:29:00 GMT</pubDate><guid>http://www.blogjava.net/asklxf/articles/22195.html</guid><wfw:comment>http://www.blogjava.net/asklxf/comments/22195.html</wfw:comment><comments>http://www.blogjava.net/asklxf/articles/22195.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/asklxf/comments/commentRss/22195.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/asklxf/services/trackbacks/22195.html</trackback:ping><description><![CDATA[<P>　　线性表，链表，哈希表是常用的数据结构，在进行Java开发时，JDK已经为我们提供了一系列相应的类来实现基本的数据结构。这些类均在java.util包中。本文试图通过简单的描述，向读者阐述各个类的作用以及如何正确使用这些类。 </P>
<P><EM>Collection</EM><BR>├<EM>List</EM><BR>│├LinkedList<BR>│├ArrayList<BR>│└Vector<BR>│　└Stack<BR>└<EM>Set</EM><BR><EM>Map</EM><BR>├Hashtable<BR>├HashMap<BR>└WeakHashMap</P>
<P><STRONG><FONT color=#0000ff>Collection接口</FONT></STRONG><BR>　　Collection是最基本的集合接口，一个Collection代表一组Object，即Collection的元素（Elements）。一些Collection允许相同的元素而另一些不行。一些能排序而另一些不行。Java SDK不提供直接继承自Collection的类，Java SDK提供的类都是继承自Collection的“子接口”如List和Set。<BR>　　所有实现Collection接口的类都必须提供两个标准的构造函数：无参数的构造函数用于创建一个空的Collection，有一个Collection参数的构造函数用于创建一个新的Collection，这个新的Collection与传入的Collection有相同的元素。后一个构造函数允许用户复制一个Collection。<BR>　　如何遍历Collection中的每一个元素？不论Collection的实际类型如何，它都支持一个iterator()的方法，该方法返回一个迭代子，使用该迭代子即可逐一访问Collection中每一个元素。典型的用法如下：<BR><FONT face="Courier New" color=#336666>　　　　Iterator it = collection.iterator(); // 获得一个迭代子<BR>　　　　while(it.hasNext()) {<BR>　　　　　　Object obj = it.next(); // 得到下一个元素<BR>　　　　}</FONT><BR>　　由Collection接口派生的两个接口是List和Set。</P>
<P><FONT color=#0000ff><STRONG>List接口</STRONG></FONT><BR>　　List是有序的Collection，使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引（元素在List中的位置，类似于数组下标）来访问List中的元素，这类似于Java的数组。<BR>和下面要提到的Set不同，List允许有相同的元素。<BR>　　除了具有Collection接口必备的iterator()方法外，List还提供一个listIterator()方法，返回一个ListIterator接口，和标准的Iterator接口相比，ListIterator多了一些add()之类的方法，允许添加，删除，设定元素，还能向前或向后遍历。<BR>　　实现List接口的常用类有LinkedList，ArrayList，Vector和Stack。</P>
<P><FONT color=#0000ff><STRONG>LinkedList类</STRONG></FONT><BR>　　LinkedList实现了List接口，允许null元素。此外LinkedList提供额外的get，remove，insert方法在LinkedList的首部或尾部。这些操作使LinkedList可被用作堆栈（stack），队列（queue）或双向队列（deque）。<BR>　　注意LinkedList没有同步方法。如果多个线程同时访问一个List，则必须自己实现访问同步。一种解决方法是在创建List时构造一个同步的List：<BR><FONT face="Courier New" color=#336666>　　　　List list = Collections.synchronizedList(new LinkedList(...));</FONT></P>
<P><FONT color=#0000ff><STRONG>ArrayList类</STRONG></FONT><BR>　　ArrayList实现了可变大小的数组。它允许所有元素，包括null。ArrayList没有同步。<BR>size，isEmpty，get，set方法运行时间为常数。但是add方法开销为分摊的常数，添加n个元素需要O(n)的时间。其他的方法运行时间为线性。<BR>　　每个ArrayList实例都有一个容量（Capacity），即用于存储元素的数组的大小。这个容量可随着不断添加新元素而自动增加，但是增长算法并没有定义。当需要插入大量元素时，在插入前可以调用ensureCapacity方法来增加ArrayList的容量以提高插入效率。<BR>　　和LinkedList一样，ArrayList也是非同步的（unsynchronized）。</P>
<P><FONT color=#0000ff><STRONG>Vector类</STRONG></FONT><BR>　　Vector非常类似ArrayList，但是Vector是同步的。由Vector创建的Iterator，虽然和ArrayList创建的Iterator是同一接口，但是，因为Vector是同步的，当一个Iterator被创建而且正在被使用，另一个线程改变了Vector的状态（例如，添加或删除了一些元素），这时调用Iterator的方法时将抛出ConcurrentModificationException，因此必须捕获该异常。</P>
<P><STRONG><FONT color=#0000ff>Stack 类</FONT></STRONG><BR>　　Stack继承自Vector，实现一个后进先出的堆栈。Stack提供5个额外的方法使得Vector得以被当作堆栈使用。基本的push和pop方法，还有peek方法得到栈顶的元素，empty方法测试堆栈是否为空，search方法检测一个元素在堆栈中的位置。Stack刚创建后是空栈。</P>
<P><FONT color=#0000ff><STRONG>Set接口</STRONG></FONT><BR>　　Set是一种不包含重复的元素的Collection，即任意的两个元素e1和e2都有e1.equals(e2)=false，Set最多有一个null元素。<BR>　　很明显，Set的构造函数有一个约束条件，传入的Collection参数不能包含重复的元素。<BR>　　请注意：必须小心操作可变对象（Mutable Object）。如果一个Set中的可变元素改变了自身状态导致Object.equals(Object)=true将导致一些问题。</P>
<P><FONT color=#0000ff><STRONG>Map接口</STRONG></FONT><BR>　　请注意，Map没有继承Collection接口，Map提供key到value的映射。一个Map中不能包含相同的key，每个key只能映射一个value。Map接口提供3种集合的视图，Map的内容可以被当作一组key集合，一组value集合，或者一组key-value映射。</P>
<P><FONT color=#0000ff><STRONG>Hashtable类</STRONG></FONT><BR>　　Hashtable继承Map接口，实现一个key-value映射的哈希表。任何非空（non-null）的对象都可作为key或者value。<BR>　　添加数据使用put(key, value)，取出数据使用get(key)，这两个基本操作的时间开销为常数。<BR>Hashtable通过initial capacity和load factor两个参数调整性能。通常缺省的load factor 0.75较好地实现了时间和空间的均衡。增大load factor可以节省空间但相应的查找时间将增大，这会影响像get和put这样的操作。<BR>使用Hashtable的简单示例如下，将1，2，3放到Hashtable中，他们的key分别是”one”，”two”，”three”：<BR><FONT face="Courier New" color=#336666>　　　　Hashtable numbers = new Hashtable();<BR>　　　　numbers.put(“one”, new Integer(1));<BR>　　　　numbers.put(“two”, new Integer(2));<BR>　　　　numbers.put(“three”, new Integer(3));</FONT><BR>　　要取出一个数，比如2，用相应的key：<BR><FONT face="Courier New" color=#336666>　　　　Integer n = (Integer)numbers.get(“two”);<BR>　　　　System.out.println(“two = ” + n);</FONT><BR>　　由于作为key的对象将通过计算其散列函数来确定与之对应的value的位置，因此任何作为key的对象都必须实现hashCode和equals方法。hashCode和equals方法继承自根类Object，如果你用自定义的类当作key的话，要相当小心，按照散列函数的定义，如果两个对象相同，即obj1.equals(obj2)=true，则它们的hashCode必须相同，但如果两个对象不同，则它们的hashCode不一定不同，如果两个不同对象的hashCode相同，这种现象称为冲突，冲突会导致操作哈希表的时间开销增大，所以尽量定义好的hashCode()方法，能加快哈希表的操作。<BR>　　如果相同的对象有不同的hashCode，对哈希表的操作会出现意想不到的结果（期待的get方法返回null），要避免这种问题，只需要牢记一条：要同时复写equals方法和hashCode方法，而不要只写其中一个。<BR>　　Hashtable是同步的。</P>
<P><STRONG><FONT color=#0000ff>HashMap类</FONT></STRONG><BR>　　HashMap和Hashtable类似，不同之处在于HashMap是非同步的，并且允许null，即null value和null key。，但是将HashMap视为Collection时（values()方法可返回Collection），其迭代子操作时间开销和HashMap的容量成比例。因此，如果迭代操作的性能相当重要的话，不要将HashMap的初始化容量设得过高，或者load factor过低。</P>
<P><FONT color=#0000ff><STRONG>WeakHashMap类</STRONG></FONT><BR>　　WeakHashMap是一种改进的HashMap，它对key实行“弱引用”，如果一个key不再被外部所引用，那么该key可以被GC回收。</P>
<P><FONT color=#ff0000><STRONG>总结</STRONG></FONT><BR>　　如果涉及到堆栈，队列等操作，应该考虑用List，对于需要快速插入，删除元素，应该使用LinkedList，如果需要快速随机访问元素，应该使用ArrayList。<BR>　　如果程序在单线程环境中，或者访问仅仅在一个线程中进行，考虑非同步的类，其效率较高，如果多个线程可能同时操作一个类，应该使用同步的类。<BR>　　要特别注意对哈希表的操作，作为key的对象要正确复写equals和hashCode方法。<BR>　　尽量返回接口而非实际的类型，如返回List而非ArrayList，这样如果以后需要将ArrayList换成LinkedList时，客户端代码不用改变。这就是针对抽象编程。</P>
<P align=right>（参考：Sun JDK1.4.1 API DOC）</P><img src ="http://www.blogjava.net/asklxf/aggbug/22195.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/asklxf/" target="_blank">Xuefeng's Weblog</a> 2005-12-02 10:29 <a href="http://www.blogjava.net/asklxf/articles/22195.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>