{"id":276,"date":"2017-01-20T14:00:25","date_gmt":"2017-01-20T06:00:25","guid":{"rendered":"http:\/\/www.prayerlaputa.com\/?p=276"},"modified":"2017-01-20T14:00:25","modified_gmt":"2017-01-20T06:00:25","slug":"%e3%80%90%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90%e3%80%91java-sdk%e7%af%87-treeset","status":"publish","type":"post","link":"http:\/\/prayerlaputa.com\/?p=276","title":{"rendered":"\u3010\u6e90\u7801\u5206\u6790\u3011java sdk\u7bc7\u2013TreeSet"},"content":{"rendered":"<p>\u4eca\u5929\u5237leetcode\u65f6\u9700\u8981\u7528\u5230TreeSet\uff0c\u7b80\u5355\u770b\u4e86\u4e0b\u6e90\u4ee3\u7801\uff0c\u5728\u6b64\u505a\u4e0b\u5b66\u4e60\u7b14\u8bb0\uff1a<\/p>\n<h2>\u6982\u5ff5<\/h2>\n<p>TreeSet\u4e0eTreeMap\u7c7b\u4f3c\uff0c\u80fd\u591f\u521b\u5efa\u6709\u5e8f\u7684\u96c6\u5408\u3002<!--more--><\/p>\n<h2>\u57fa\u672c\u64cd\u4f5c<\/h2>\n<p>TreeSet\u7684\u5b9e\u73b0\u57fa\u4e8eTreeMap\uff08TreeMap\u6e90\u7801\u6bd4\u8f83\u590d\u6742\uff0c\u53c2\u8003\u8fd9\u7bc7\u6587\u7ae0\uff1a<span class=\"link_title\"><a href=\"http:\/\/blog.csdn.net\/chenssy\/article\/details\/26668941\">Java\u63d0\u9ad8\u7bc7\uff08\u4e8c\u4e03\uff09&#8212;&#8211;TreeMap<\/a>\u00a0<\/span>\uff09\uff0c\u5185\u90e8\u4f7f\u7528<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nprivate transient NavigableMap&lt;E,Object&gt; m;\n<\/pre>\n<p>\u8fd9\u4e2aNavigableMap\u4fdd\u5b58\u6570\u636e\uff0c\u800c\u5b9e\u9645\u4e0a\u8fd9\u4e2am\u5c31\u662f\u4e00\u4e2aTreeMap\u5bf9\u8c61\uff0c\u800c\u5728\u8fdb\u884cadd(E)\u64cd\u4f5c\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u8fdb\u884c\u4e86\u5982\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nm.put(E, dummyObject);\n<\/pre>\n<p>\u7531\u4e8e\u57fa\u4e8eTreeMap\uff0cTreeSet\u7684add\/remove\/contains\u7b49\u57fa\u672c\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u540c\u6837\u4e3alog(n)\u3002<\/p>\n<h2>\u540c\u6b65<\/h2>\n<p>\u4e0eTreeMap\u4e00\u6837\uff0cTreeSet\u7684\u57fa\u672c\u64cd\u4f5c\u5e76\u6ca1\u6709\u505a\u7ebf\u7a0b\u5b89\u5168\u5904\u7406\uff0c\u56e0\u6b64\u9700\u8981\u591a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u4e2aTreeSet\u5bf9\u8c61\u3001\u5e76\u4e14\u81f3\u5c11\u6709\u4e00\u4e2a\u7ebf\u7a0b\u9700\u8981\u4fee\u6539\u8be5\u5bf9\u8c61\u65f6\uff0c\u9700\u8981\u624b\u52a8\u52a0\u4e0a\u540c\u6b65\u8bed\u53e5\uff0c\u6bd4\u5982\u4f7f\u7528synchronized\u5173\u952e\u5b57\uff0c\u9664\u6b64\u4e4b\u5916\u8fd8\u53ef\u4ee5\u5728\u521b\u5efaTreeSet\u5bf9\u8c61\u65f6\u4f7f\u7528Collections.synchronizedSortedSet\u65b9\u6cd5\u6765\u4fdd\u8bc1\u540c\u6b65\u5b89\u5168\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nSortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));\n<\/pre>\n<p>\u6b64\u5916\uff0cTreeSet\u6240\u8fd4\u56de\u7684iterator\u662ffail-fast\u7684\uff0c\u6e90\u7801\u4e2d\u89e3\u91ca\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>The iterators returned by this class&#8217;s iterator method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator&#8217;s own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.<br \/>\nNote that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.<\/p><\/blockquote>\n<p>\u6709\u5173fail-fast\uff0c\u53ef\u4ee5\u53c2\u8003\u8fd9\u7bc7\u6587\u7ae0\uff1a<a href=\"http:\/\/blog.csdn.net\/chenssy\/article\/details\/38151189\">Java\u63d0\u9ad8\u7bc7\uff08\u4e09\u56db\uff09&#8212;&#8211;fail-fast\u673a\u5236<\/a><br \/>\n\u6709\u5173TreeSet\u66f4\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u53c2\u8003\u8fd9\u7bc7\u6587\u7ae0\uff1a<a id=\"cb_post_title_url\" class=\"singleposttitle\" href=\"http:\/\/www.cnblogs.com\/skywang12345\/p\/3311268.html\">Java \u96c6\u5408\u7cfb\u521717\u4e4b TreeSet\u8be6\u7ec6\u4ecb\u7ecd(\u6e90\u7801\u89e3\u6790)\u548c\u4f7f\u7528\u793a\u4f8b<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5237leetcode\u65f6\u9700\u8981\u7528\u5230TreeSet\uff0c\u7b80\u5355\u770b\u4e86\u4e0b\u6e90\u4ee3\u7801\uff0c\u5728\u6b64\u505a\u4e0b\u5b66\u4e60\u7b14\u8bb0\uff1a \u6982\u5ff5 TreeSet\u4e0eT <a href='http:\/\/prayerlaputa.com\/?p=276' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[6,12,18],"tags":[33,50],"_links":{"self":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/276"}],"collection":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=276"}],"version-history":[{"count":0,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/276\/revisions"}],"wp:attachment":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}