





{"id":1577,"date":"2010-11-29T01:39:49","date_gmt":"2010-11-29T05:39:49","guid":{"rendered":"http:\/\/www.killtest.hk\/?p=1577"},"modified":"2010-11-29T01:43:18","modified_gmt":"2010-11-29T05:43:18","slug":"enumeration","status":"publish","type":"post","link":"https:\/\/www.killtest.hk\/index.php\/3com\/enumeration\/","title":{"rendered":"\u901a\u904eEnumeration\u548cIterator\u904d\u6b77Hashtable\u7684\u6548\u7387\u5206\u6790"},"content":{"rendered":"<p>\u4eca\u5929\u9700\u8981\u904d\u6b77\u58f9\u500bHashtable\uff0c\u67e5\u770b\u4e86\u58f9\u4e0bHashtable\u985e\uff0c\u767c\u73fe\u5b83\u63d0\u4f9b\u4e86\u5982\u4e0b\u5e7e\u500b\u65b9\u6cd5\u53ef\u4f9b\u6211\u5011\u904d\u6b77\uff1a<\/p>\n<p>\u3000\u3000keys() &#8211; returns an Enumeration of the keys of this Hashtable<\/p>\n<p>\u3000\u3000keySet() &#8211; returns a Set of the keys<\/p>\n<p>\u3000\u3000entrySet() &#8211; returns a Set of the mappings<\/p>\n<p>\u3000\u3000elements() &#8211; returns an Enumeration of the values of this Hashtable<\/p>\n<p>\u3000\u30004\u7a2e\u65b9\u6cd5\uff0c\u90a3\u7a2e\u66f4\u597d\u5462\uff0c\u5beb\u6bb5\u4ee3\u78bc\u4f86\u6bd4\u8f03\u58f9\u4e0b\u5427\uff1a<\/p>\n<p>\u3000\u3000import java.util.Enumeration;<\/p>\n<p>\u3000\u3000import java.util.Hashtable;<\/p>\n<p>\u3000\u3000import java.util.Iterator;<\/p>\n<p>\u3000\u3000import java.util.Map.Entry;<\/p>\n<p>\u3000\u3000public class traveseHashTable {<\/p>\n<p>\u3000\u3000public static void main(String[] args) {<\/p>\n<p>\u3000\u3000Hashtable&lt;String, String&gt; ht = new Hashtable&lt;String, String&gt;();<\/p>\n<p>\u3000\u3000for (int i = 0; i &lt; 10000; i++) {<\/p>\n<p>\u3000\u3000ht.put(&#8220;Key=&#8221; + i, &#8220;Val=&#8221; + i);<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000\/\/ 1. Enumeration<\/p>\n<p>\u3000\u3000long start = System.currentTimeMillis();<\/p>\n<p>\u3000\u3000Enumeration&lt;String&gt; en = ht.keys();<\/p>\n<p>\u3000\u3000while (en.hasMoreElements()) {<\/p>\n<p>\u3000\u3000en.nextElement();<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000long end = System.currentTimeMillis();<\/p>\n<p>\u3000\u3000System.out.println(&#8220;Enumeration keys costs &#8221; + (end &#8211; start)<\/p>\n<p>\u3000\u3000+ &#8221; milliseconds&#8221;);<\/p>\n<p>\u3000\u3000\/\/ 2. Enumeration<\/p>\n<p>\u3000\u3000start = System.currentTimeMillis();<\/p>\n<p>\u3000\u3000Enumeration&lt;String&gt; en2 = ht.elements();<\/p>\n<p>\u3000\u3000while (en2.hasMoreElements()) {<\/p>\n<p>\u3000\u3000en2.nextElement();<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000end = System.currentTimeMillis();<\/p>\n<p>\u3000\u3000System.out.println(&#8220;Enumeration elements costs &#8221; + (end &#8211; start) + &#8221; milliseconds&#8221;);<\/p>\n<p>\u3000\u3000\/\/ 3. Iterator<\/p>\n<p>\u3000\u3000start = System.currentTimeMillis();<\/p>\n<p>\u3000\u3000Iterator&lt;String&gt; it = ht.keySet().iterator();<\/p>\n<p>\u3000\u3000while (it.hasNext()) {<\/p>\n<p>\u3000\u3000it.next();<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000end = System.currentTimeMillis();<\/p>\n<p>\u3000\u3000System.out.println(&#8220;Iterator keySet costs &#8221; + (end &#8211; start) + &#8221; milliseconds&#8221;);<\/p>\n<p>\u3000\u3000\/\/ 4. Iterator<\/p>\n<p>\u3000\u3000start = System.currentTimeMillis();<\/p>\n<p>\u3000\u3000Iterator&lt;Entry&lt;String, String&gt;&gt; it2 = ht.entrySet().iterator();<\/p>\n<p>\u3000\u3000while (it2.hasNext()) {<\/p>\n<p>\u3000\u3000it2.next();<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000end = System.currentTimeMillis();<\/p>\n<p>\u3000\u3000System.out.println(&#8220;Iterator entrySet costs &#8221; + (end &#8211; start) + &#8221; milliseconds&#8221;);<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000\u9019\u88cf\u5275\u5efa\u4e86\u58f9\u500b10000\u500b\u5143\u7d20\u7684Hashtable\u4f86\u4f9b\u6211\u5011\u904d\u6b77\uff0c\u6253\u5370\u51fa\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<p>\u3000\u3000Enumeration keys costs 6 milliseconds<\/p>\n<p>\u3000\u3000Enumeration elements costs 5 milliseconds<\/p>\n<p>\u3000\u3000Iterator keySet costs 10 milliseconds<\/p>\n<p>\u3000\u3000Iterator entrySet costs 10 milliseconds<\/p>\n<p>\u3000\u3000\u6211\u5011\u770b\u5230\uff0c\u901a\u904e\u53e0\u4ee3\u4f86\u904d\u6b77\u6bd4\u679a\u8209\u8981\u591a\u82b1\u76e1\u58f9\u500d\u7684\u6642\u9593\u3002\u6240\u4ee5\u5efa\u8b70\u5927\u5bb6\u6700\u597d\u901a\u904e\u679a\u8209\u985e\u4f86\u904d\u6b77Hashtable\u54e6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u9700\u8981\u904d\u6b77\u58f9\u500bHashtable\uff0c\u67e5\u770b\u4e86\u58f9\u4e0bHashtable\u985e\uff0c\u767c\u73fe\u5b83\u63d0\u4f9b\u4e86\u5982\u4e0b\u5e7e\u500b\u65b9\u6cd5\u53ef\u4f9b\u6211\u5011\u904d\u6b77\uff1a \u3000 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[632,634,633],"class_list":["post-1577","post","type-post","status-publish","format-standard","hentry","category-3com","tag-enumeration","tag-hashtable","tag-iterator"],"_links":{"self":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts\/1577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/comments?post=1577"}],"version-history":[{"count":0,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts\/1577\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/media?parent=1577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/categories?post=1577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/tags?post=1577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}