{"id":606,"date":"2022-01-12T13:36:23","date_gmt":"2022-01-12T05:36:23","guid":{"rendered":"https:\/\/bigwer.cn\/?p=606"},"modified":"2022-03-10T18:37:27","modified_gmt":"2022-03-10T10:37:27","slug":"jdk1-8-set","status":"publish","type":"post","link":"https:\/\/bigwer.cn\/index.php\/2022\/01\/12\/jdk1-8-set\/","title":{"rendered":"JDK1.8-Set"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h3>\u4e00.Set<\/h3>\n<h4>1.\u6cdb\u578b<\/h4>\n<ol>\n<li>jdk1.5\u5f15\u5165\uff0c\u4e4b\u524d\u90fd\u662f\u4f7f\u7528Object[]<\/li>\n<li>\u4f7f\u7528Object[]\u7684\u7f3a\u70b9\n<ul>\n<li>\u83b7\u53d6\u4e00\u4e2a\u503c\u65f6\uff0c\u5fc5\u987b\u8fdb\u884c\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362<\/li>\n<li>\u8c03\u7528\u4e00\u4e2a\u65b9\u6cd5\u524d\uff0c\u5fc5\u987b\u4f7f\u7528instanceof\u5224\u65ad\u5bf9\u8c61\u7c7b\u578b<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h4>2.\u6cdb\u578b\u7684\u597d\u5904<\/h4>\n<ol>\n<li>\u51cf\u5c11\u4e86\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u7684\u6b21\u6570<\/li>\n<li>\u83b7\u53d6\u6570\u636e\u503c\u66f4\u52a0\u65b9\u4fbf<\/li>\n<li>\u8c03\u7528\u65b9\u6cd5\u65f6\u66f4\u52a0\u5b89\u5168<\/li>\n<li>\u6cdb\u578b\u53ea\u5728\u7f16\u8bd1\u65f6\u671f\u8d77\u4f5c\u7528\uff0c\u8fd0\u884c\u9636\u6bb5JVM\u770b\u4e0d\u89c1\u6cdb\u578b\u7c7b\u578b(\u56e0\u4e3a\u8fdb\u884c\u4e86\u7c7b\u578b\u64e6\u9664\u6240\u4ee5JVM\u53ea\u80fd\u770b\u89c1\u5bf9\u5e94\u7684\u539f\u59cb\u7c7b\u578b)<\/li>\n<li>\u5728\u4f7f\u7528\u65f6\u6ca1\u6709\u6307\u5b9a\u6cdb\u578b\u7c7b\u578b\u65f6\uff0c\u9ed8\u8ba4\u4f7f\u7528Object\u7c7b<\/li>\n<li>\u652f\u6301lambda\u8868\u8fbe\u5f0f<\/li>\n<\/ol>\n<h3>\u4e8c.HashSet<\/h3>\n<h4>1.\u7279\u70b9<\/h4>\n<p>HashSet\u662f\u65e0\u5e8f\u7684(\u65e0\u4e0b\u6807)\uff0c\u5e76\u4e14\u6570\u636e\u4e0d\u53ef\u91cd\u590d\uff0c\u4ed6\u7684\u5e95\u5c42\u7ef4\u62a4\u7684\u662fHashMap\u4e2dkey\u7684\u90e8\u5206\uff0c\u5141\u8bb8\u5b58\u5165null\u5143\u7d20\u3002<\/p>\n<h4>2.\u5c5e\u6027<\/h4>\n<pre><code class=\"language-java\">    \/\/\u5e8f\u5217\u5316ID\n    static final long serialVersionUID = -5024744406713321676L;\n    \/\/\u5e95\u5c42\u4f7f\u7528HashMap\u6765\u4fdd\u5b58\u6570\u636e\n    private transient HashMap&lt;E,Object&gt; map;\n    \/\/\u7531\u4e8eSet\u53ea\u4f7f\u7528\u5230\u4e86HashMap\u7684key\uff0c\u6240\u4ee5\u6b64\u5904\u5b9a\u4e49\u4e00\u4e2a\u9759\u6001\u7684\u5e38\u91cfObject\u7c7b\uff0c\u6765\u5145\u5f53HashMap\u7684value\n    private static final Object PRESENT = new Object();<\/code><\/pre>\n<h4>3.\u5185\u90e8\u65b9\u6cd5<\/h4>\n<h5>(1).\u6784\u9020\u65b9\u6cd5<\/h5>\n<pre><code class=\"language-java\">\/\/\u4f7f\u7528HashMap\u7684\u9ed8\u8ba4\u5bb9\u91cf\u5927\u5c0f16\u548c\u9ed8\u8ba4\u6269\u5bb9\u56e0\u5b500.75\u521d\u59cb\u5316map\uff0c\u6784\u9020\u4e00\u4e2aHashSet\npublic HashSet() {\n        map = new HashMap&lt;&gt;();\n    }\n\/\/\u6784\u9020\u4e00\u4e2a\u6307\u5b9aCollection\u53c2\u6570\u7684HashSet\uff0c\u8fd9\u91cc\u4e0d\u4ec5\u4ec5\u662fSet\uff0c\u53ea\u8981\u5b9e\u73b0Collection\u63a5\u53e3\u7684\u5bb9\u5668\u90fd\u53ef\u4ee5\npublic HashSet(Collection&lt;? extends E&gt; c) {\n        map = new HashMap&lt;&gt;(Math.max((int) (c.size()\/.75f) + 1, 16));\n        \/\/\u4f7f\u7528Collection\u5b9e\u73b0\u7684Iterator\u8fed\u4ee3\u5668\uff0c\u5c06\u96c6\u5408c\u7684\u5143\u7d20\u4e00\u4e2a\u4e2a\u52a0\u5165HashSet\u4e2d\n        addAll(c);\n    }\npublic boolean addAll(Collection&lt;? extends E&gt; c) {\n        boolean modified = false;\n        for (E e : c)\n            if (add(e))\n                modified = true;\n        return modified;\n    }\n\/\/\u4f7f\u7528\u6307\u5b9a\u7684\u521d\u59cb\u5bb9\u91cf\u5927\u5c0f\u548c\u6269\u5bb9\u56e0\u5b50\u521d\u59cb\u5316map\uff0c\u6784\u9020\u4e00\u4e2aHashSet\npublic HashSet(int initialCapacity, float loadFactor) {\n        map = new HashMap&lt;&gt;(initialCapacity, loadFactor);\n    }\n\/\/\u4f7f\u7528\u6307\u5b9a\u7684\u521d\u59cb\u5bb9\u91cf\u5927\u5c0f\u548c\u9ed8\u8ba4\u7684\u6269\u5bb9\u56e0\u5b500.75\u521d\u59cb\u5316map\uff0c\u6784\u9020\u4e00\u4e2aHashSet\npublic HashSet(int initialCapacity) {\n        map = new HashMap&lt;&gt;(initialCapacity);\n    }\n\/\/\u4e0d\u5bf9\u5916\u516c\u5f00\u7684\u4e00\u4e2a\u6784\u9020\u65b9\u6cd5\uff08\u9ed8\u8ba4default\u4fee\u9970\uff09\uff0c\u5e95\u5c42\u6784\u9020\u7684\u662fLinkedHashMap\uff0cdummy\u53ea\u662f\u4e00\u4e2a\u6807\u793a\u53c2\u6570\uff0c\u65e0\u5177\u4f53\u610f\u4e49\nHashSet(int initialCapacity, float loadFactor, boolean dummy) {\n        map = new LinkedHashMap&lt;&gt;(initialCapacity, loadFactor);\n    }<\/code><\/pre>\n<h5>(2).add\u65b9\u6cd5<\/h5>\n<pre><code class=\"language-java\">\/\/\u4f7f\u7528HashMap\u7684add\u65b9\u6cd5\uff0c\u5c06\u4f20\u5165\u7684\u503c\u5b58\u4e3amap\u7684key,value\u4f7f\u7528\u4e00\u4e2a\u9759\u6001\u503c\u586b\u5145\npublic boolean add(E e) {\n        return map.put(e, PRESENT)==null;\n    }<\/code><\/pre>\n<h5>(3).remove\u65b9\u6cd5<\/h5>\n<pre><code class=\"language-java\">\/\/\u8c03\u7528HashMap\u7684remove\u65b9\u6cd5\u5b9e\u73b0\u5220\u9664\n public boolean remove(Object o) {\n        return map.remove(o)==PRESENT;\n    }<\/code><\/pre>\n<h5>(4)contains\u65b9\u6cd5<\/h5>\n<pre><code class=\"language-java\">\/\/\u8c03\u7528HashMap\u7684containsKey\u65b9\u6cd5\u5224\u65ad\u5143\u7d20\u662f\u5426\u5b58\u5728\npublic boolean contains(Object o) {\n        return map.containsKey(o);\n    }<\/code><\/pre>\n<h3>\u4e09.TreeSet<\/h3>\n<h4>1.\u7279\u70b9<\/h4>\n<p>TreeSet\u662f\u65e0\u5e8f\u7684(\u65e0\u4e0b\u6807)\uff0c\u5e76\u4e14\u6570\u636e\u4e0d\u53ef\u91cd\u590d\uff0c\u4f46\u662f\u53ef\u4ee5\u6392\u5e8f\uff0c\u4ed6\u7684\u5e95\u5c42\u4e00\u822c\u90fd\u662f\u7ef4\u62a4\u4e86\u4e00\u4e2aTreeMap(\u57fa\u4e8e\u6784\u9020\u65b9\u6cd5)\u3002<\/p>\n<h4>2.\u5c5e\u6027<\/h4>\n<pre><code class=\"language-java\">    \/*\n    TreeMap\u5b9e\u73b0\u4e86NavigableMap\u63a5\u53e3\n    \u5b58\u50a8\u7684\u662f\u4e00\u4e2aNavigableMap\u3002\n    TreeMap\u5b9e\u73b0\u4e86NavigableMap\u3002\n    \u6240\u4ee5TreeSet\u5185\u90e8\u4e0d\u4e00\u5b9a\u5c31\u662f\u4f7f\u7528\u7684TreeMap\u3002\n    *\/\n    private transient NavigableMap&lt;E,Object&gt; m;\n    \/\/\u9759\u6001\u5e38\u91cfObject\u7c7b\n    private static final Object PRESENT = new Object();<\/code><\/pre>\n<h4>3.\u5185\u90e8\u65b9\u6cd5<\/h4>\n<h5>(1).\u6784\u9020\u65b9\u6cd5<\/h5>\n<pre><code class=\"language-java\">\/\/\u901a\u8fc7NavigableMap\u76f4\u63a5\u8d4b\u503c\u7ed9TreeSet\u5185\u90e8\u7684m,\u6b64\u65f6\u5185\u90e8\u4f7f\u7528\u7684\u5c31\u662fNavigableMap\u3002\nTreeSet(NavigableMap&lt;E,Object&gt; m) {\n        this.m = m;\n    }\n\/\/\u65b0\u5efa\u4e00\u4e2aTreeMap\u4f5c\u4e3a\u5e95\u5c42\u5b58\u50a8\u7a7a\u95f4\uff0ckey\u4e3a\u6cdb\u578b\uff0c\u56e0\u4e3a\u4e0d\u4f7f\u7528\u5230value\u6240\u4ee5\u5b9a\u4e49\u4e3a\u4e00\u4e2aObject\u7c7b\u7528PRESENT\u6765\u586b\u5145\npublic TreeSet() {\n        this(new TreeMap&lt;E,Object&gt;());\n    }\n\/\/\u901a\u8fc7\u4f20\u5165\u4e00\u4e2a\u6bd4\u8f83\u5668\u6784\u5efaTreeSet(\u7528\u4e8e\u6392\u5e8f)\npublic TreeSet(Comparator&lt;? super E&gt; comparator) {\n        this(new TreeMap&lt;&gt;(comparator));\n    }\n\/\/\u6784\u9020\u4e00\u4e2a\u6307\u5b9aCollection\u53c2\u6570\u7684TreeSet,\u901a\u8fc7addAll\u5c06\u6240\u6709\u503c\u4f20\u5165\npublic TreeSet(Collection&lt;? extends E&gt; c) {\n        this();\n        addAll(c);\n    }\n\/\/\u901a\u8fc7\u4f20\u5165\u7684\u6392\u5e8f\u96c6\u5408\u521b\u5efa\u4e00\u4e2a\u76f8\u540c\u6392\u5e8f\u7684\u96c6\u5408\npublic TreeSet(SortedSet&lt;E&gt; s) {\n        this(s.comparator());\n        addAll(s);\n    }<\/code><\/pre>\n<h5>(2).add\u65b9\u6cd5<\/h5>\n<pre><code class=\"language-java\">\/\/\u53ea\u4f7f\u7528Key\u6765\u5b58\u50a8\u6570\u636e\npublic boolean add(E e) {\n        return m.put(e, PRESENT)==null;\n    }<\/code><\/pre>\n<h5>(3).addAll\u65b9\u6cd5<\/h5>\n<pre><code class=\"language-java\">\/*\n\u5982\u679cTreeSet\u662f\u7a7a\u7684\uff0c\u8c03\u7528\u4e86addAll\u65b9\u6cd5\u540e\uff0c\u4f1a\u76f4\u63a5\u521b\u5efa\u4e00\u4e2aTreeMap\n*\/\npublic  boolean addAll(Collection&lt;? extends E&gt; c) {\n        \/\/ Use linear-time version if applicable\n        if (m.size()==0 &amp;&amp; c.size() &gt; 0 &amp;&amp;\n            c instanceof SortedSet &amp;&amp;\n            m instanceof TreeMap) {\n            SortedSet&lt;? extends E&gt; set = (SortedSet&lt;? extends E&gt;) c;\n            TreeMap&lt;E,Object&gt; map = (TreeMap&lt;E, Object&gt;) m;\n            Comparator&lt;?&gt; cc = set.comparator();\n            Comparator&lt;? super E&gt; mc = map.comparator();\n            if (cc==mc || (cc != null &amp;&amp; cc.equals(mc))) {\n                map.addAllForTreeSet(set, PRESENT);\n                return true;\n            }\n        }\n        return super.addAll(c);\n    }<\/code><\/pre>\n<h5>(4).remove\u65b9\u6cd5<\/h5>\n<pre><code class=\"language-java\">\/\/\u8c03\u7528\u5e95\u5c42map\u7684remove\u65b9\u6cd5\u8fdb\u884c\u5220\u9664\npublic boolean remove(Object o) {\n        return m.remove(o)==PRESENT;\n    }<\/code><\/pre>\n<h3>\u56db.LinkedHashSet<\/h3>\n<h4>1.\u7279\u70b9<\/h4>\n<p>LinkedHashSet\u662fSet\u96c6\u5408\u7684\u4e00\u4e2a\u5b9e\u73b0\uff0c\u7ee7\u627f\u4e86HashSet\uff0c\u5177\u6709Set\u96c6\u5408\u4e0d\u91cd\u590d\u7684\u7279\u70b9\uff0c\u56e0\u4e3a\u91c7\u7528\u4e86\u53cc\u5411\u94fe\u8868\uff0c\u6240\u4ee5\u5176\u987a\u5e8f\u5c31\u662f\u63d2\u5165\u987a\u5e8f\u3002<\/p>\n<h4>2.\u6784\u9020\u65b9\u6cd5<\/h4>\n<pre><code class=\"language-java\">\/\/\u90fd\u662f\u8c03\u7528\u7236\u7c7b(HashSet)\u6765\u6784\u9020\u7684\uff0cHashSet\u7684\u5e95\u5c42\u5b9e\u9645\u4e0a\u662fHashMap\uff0c\u6240\u4ee5\u4f20\u5165\u4e00\u4e2a\u5bb9\u91cf\u4e3a16\uff0c\u6269\u5bb9\u56e0\u5b50\u4e3a0.75\npublic LinkedHashSet(int initialCapacity, float loadFactor) {\n        super(initialCapacity, loadFactor, true);\n    }\n\npublic LinkedHashSet(int initialCapacity) {\n        super(initialCapacity, .75f, true);\n    }\n\npublic LinkedHashSet() {\n        super(16, .75f, true);\n    }\n\npublic LinkedHashSet(Collection&lt;? extends E&gt; c) {\n        super(Math.max(2*c.size(), 11), .75f, true);\n        addAll(c);\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.Set 1.\u6cdb\u578b jdk1.5\u5f15\u5165\uff0c\u4e4b\u524d\u90fd\u662f\u4f7f\u7528Object[] \u4f7f\u7528Object[]\u7684\u7f3a\u70b9 \u83b7\u53d6\u4e00\u4e2a\u503c\u65f6\uff0c\u5fc5\u987b\u8fdb\u884c\u5f3a\u5236\u7c7b\u578b\u8f6c &#8230;<\/p>","protected":false},"author":1,"featured_media":610,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[46,48,45,47],"_links":{"self":[{"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/posts\/606"}],"collection":[{"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/comments?post=606"}],"version-history":[{"count":3,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/posts\/606\/revisions"}],"predecessor-version":[{"id":611,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/posts\/606\/revisions\/611"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/media\/610"}],"wp:attachment":[{"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/media?parent=606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/categories?post=606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/tags?post=606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}