{"id":47918,"date":"2023-12-12T10:34:33","date_gmt":"2023-12-12T02:34:33","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=47918"},"modified":"2023-12-12T10:35:13","modified_gmt":"2023-12-12T02:35:13","slug":"java-ji-he-qu-zhong","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/12\/java-ji-he-qu-zhong\/","title":{"rendered":"Java\u96c6\u5408\u53bb\u91cd"},"content":{"rendered":"<pre><code class=\"language-java\">package com.kaifamiao;\n\nimport java.util.*;\nimport java.util.function.Consumer;\nimport java.util.function.Function;\nimport java.util.stream.Collectors;\n\npublic class Main {\n    public static void main(String[] args) {\n        List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;() {{\n            for (int i = 0; i &lt; 100000; i++) {\n                add(13);add(3);add(34);add(79);add(118);\n                add(34);add(11);add(34);\n            }\n\n        }};\n\/\/        System.out.println(&quot;\u539f\u96c6\u5408:&quot; + list);\n        System.out.println(&quot;\u539f\u96c6\u5408\u5927\u5c0f:&quot; + list.size());\n\n        exec(Main::qcHashSet,list);\n        exec(Main::qcTreeSet,list);\n        exec(Main::qcLinkedHashSet,list);\n        exec(Main::qcIterator,list);\n        exec(Main::qcStream,list);\n        exec(Main::qcContains,list);\n\n\n\n    }\n    \/\/exec\u51fd\u6570\u7684\u53c2\u6570\u662f\u51fd\u6570\uff0c\u7136\u540e\u8fd0\u884c\u8be5\u51fd\u6570,\u51fd\u6570\u6709\u4e2a\u53c2\u6570\u662flist\n\n    public static void exec(Consumer&lt;List&gt; function, List&lt;Integer&gt; parameter) {\n        \/\/\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\n        long startTime = System.nanoTime();\n        \/\/ \u8c03\u7528\u4f20\u5165\u7684\u51fd\u6570\uff0c\u4f20\u5165\u5217\u8868\n        function.accept(parameter);\n        long endTime = System.nanoTime();\n        System.out.println(&quot;\\t\\t\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1a&quot; + (endTime - startTime) + &quot; \u7eb3\u79d2&quot;);\n\n    }\n\n    public static void qcHashSet(List&lt;Integer&gt; list) {\n        HashSet&lt;Integer&gt; set = new HashSet&lt;&gt;(list);\n        System.out.print(&quot;HashSet \u53bb\u91cd\u96c6\u5408:&quot; + set);\n    }\n\n    public static void qcTreeSet(List&lt;Integer&gt; list) {\n        TreeSet&lt;Integer&gt; set = new TreeSet&lt;&gt;(list);\n        System.out.print(&quot;TreeSe \u53bb\u91cd\u96c6\u5408:&quot; + set);\n    }\n    public static void qcLinkedHashSet(List&lt;Integer&gt; list) {\n        LinkedHashSet&lt;Integer&gt; set = new LinkedHashSet&lt;&gt;(list);\n        System.out.print(&quot;LinkedHashSet \u53bb\u91cd\u96c6\u5408:&quot; + set);\n    }\n\n    public static void qcIterator(List&lt;Integer&gt; list) {\n        Iterator&lt;Integer&gt; iterator = list.iterator();\n        while (iterator.hasNext()) {\n            \/\/ \u83b7\u53d6\u5faa\u73af\u7684\u503c\n            Integer item = iterator.next();\n            \/\/ \u5982\u679c\u5b58\u5728\u4e24\u4e2a\u76f8\u540c\u7684\u503c\n            if (list.indexOf(item) != list.lastIndexOf(item)) {\n                \/\/ \u79fb\u9664\u6700\u540e\u90a3\u4e2a\u76f8\u540c\u7684\u503c\n                iterator.remove();\n            }\n        }\n        System.out.print(&quot;Iterator \u53bb\u91cd\u96c6\u5408:&quot; + list);\n    }\n\n    public static void qcStream(List&lt;Integer&gt; list) {\n        list = list.stream().distinct().collect(Collectors.toList());\n        System.out.print(&quot;Stream \u53bb\u91cd\u96c6\u5408:&quot; + list);\n    }\n\n    public static void qcContains(List&lt;Integer&gt; list) {\n        List&lt;Integer&gt; newList = new ArrayList&lt;&gt;(list.size());\n        list.forEach(i -&gt; {\n            if (!newList.contains(i)) { \/\/ \u5982\u679c\u65b0\u96c6\u5408\u4e2d\u4e0d\u5b58\u5728\u5219\u63d2\u5165\n                newList.add(i);\n            }\n        });\n        System.out.print(&quot;contains \u53bb\u91cd\u96c6\u5408:&quot; + newList);\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>package com.kaifamiao; import java.util.*; import java. [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118],"tags":[],"class_list":["post-47918","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/47918","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=47918"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/47918\/revisions"}],"predecessor-version":[{"id":47919,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/47918\/revisions\/47919"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=47918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=47918"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=47918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}