{"id":10008235,"date":"2023-09-06T16:43:43","date_gmt":"2023-09-06T08:43:43","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008235"},"modified":"2023-09-06T16:43:43","modified_gmt":"2023-09-06T08:43:43","slug":"%e4%b8%8d%e5%a5%bd%e6%84%8f%e6%80%9d%ef%bc%8clist-contain-%e5%8e%bb%e9%87%8d%e8%af%a5%e6%8d%a2%e6%8d%a2%e4%ba%86%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008235","title":{"rendered":"\u4e0d\u597d\u610f\u601d\uff0clist.contain \u53bb\u91cd\u8be5\u6362\u6362\u4e86\uff01"},"content":{"rendered":"<p>\u6700\u8fd1\u53c8\u662f\u4e00\u8f6e\u4ee3\u7801review , \u53d1\u73b0\u4e86\u4e00\u4e9b\u5b9e\u73b0\u53bb\u91cd\u7684\u4ee3\u7801\uff0c\u5728\u4f7f\u7528 list.contain &#8230;&#8230;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"210\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9ztT\" alt=\"\" title=\"\"><\/p>\n<p>\u6211\u6c89\u601d\uff0c\u662f\u4e0d\u662f\u5176\u5b9e\u5f88\u591a\u521d\u5b66\u8005\u4e5f\u5b58\u5728\u8fd9\u79cd\u53bb\u91cd\u4f7f\u7528\u95ee\u9898\uff1f<\/p>\n<p>\u6240\u4ee5\u6211\u9009\u62e9\u628a\u8fd9\u4e2a\u4e8b\u60c5\u6574\u51fa\u6765\uff0c\u5206\u4eab\u4e00\u4e0b\u3002<\/p>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<\/p>\n<blockquote><p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=j4gLwXo7wOYFHKTsJ2ICyg%3D%3D.ocYfXo30pkBh3dAXoPdGGnAYEbsntBArKfbHBpdKFgycEvo3lEdlAi3UxknP%2FNhzPzB0UbpEHIPM%2FbA99Wx4aQ%3D%3D\" rel=\"noopener noreferrer\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p><\/blockquote>\n<h2>\u2460<\/h2>\n<p>\u9996\u5148\u662f\u9020\u51fa\u4e00\u4e2a List&lt;String&gt; \u6a21\u62df\u6570\u636e\uff0c\u4e00\u51712W\u6761\uff0c\u91cc\u9762\u6709\u4e00\u534a\u6570\u636e1W\u6761\u662f\u91cd\u590d\u7684:<\/p>\n<pre><code>public static List&lt;String&gt; getTestList() {\n    List&lt;String&gt; list = new ArrayList&lt;&gt;();\n    for (int i = 1; i &lt;= 10000; i++) {\n        list.add(String.valueOf(i));\n    }\n    for (int i = 10000; i &gt;= 1; i--) {\n        list.add(String.valueOf(i));\n    }\n    return list;\n}<\/code><\/pre>\n<p>\u5148\u770b\u770b\u6211\u4eec\u7528 contain \u53bb\u91cd\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code>\/**\n * \u4f7f\u7528 list.contain \u53bb\u91cd\n *\n * @param testList\n *\/\nprivate static void useContain2Distinct(List&lt;String&gt; testList) {\n    System.out.println(&quot;contains \u5f00\u59cb\u53bb\u91cd\uff0c\u6761\u6570\uff1a&quot; + testList.size());\n    List&lt;String&gt; testListDistinctResult = new ArrayList&lt;&gt;();\n    for (String str : testList) {\n        if (!testListDistinctResult.contains(str)) {\n            testListDistinctResult.add(str);\n        }\n    }\n    System.out.println(&quot;contains \u53bb\u91cd\u5b8c\u6bd5\uff0c\u6761\u6570\uff1a&quot; + testListDistinctResult.size());\n}<\/code><\/pre>\n<p>\u6211\u4eec\u8c03\u7528\u4e00\u4e0b\u770b\u770b\u8017\u65f6\uff1a<\/p>\n<pre><code>public static void main(String[] args) {\n    List&lt;String&gt; testList = getTestList();\n    StopWatch stopWatch = new StopWatch();\n    stopWatch.start();\n    useContainDistinct(testList);\n    stopWatch.stop();\n    System.out.println(&quot;\u53bb\u91cd \u6700\u7ec8\u8017\u65f6&quot; + stopWatch.getTotalTimeMillis());\n}<\/code><\/pre>\n<p>\u8017\u65f6\uff1a<\/p>\n<p><img decoding=\"async\" width=\"531\" height=\"141\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9ztU\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u8bc4\u4ef7\uff1a<\/strong>list.contain \u7684\u6548\u7387\uff0c\u6211\u7684\u5efa\u8bae\u662f\uff0c\u77e5\u9053\u5c31\u884c\uff0c\u522b\u7528\u3002<\/p>\n<h2>\u2461<\/h2>\n<p>\u4f17\u6240\u5468\u77e5 Set \u4e0d\u5b58\u5728\u91cd\u590d\u6570\u636e\uff0c \u6240\u4ee5\u6211\u4eec\u6765\u770b\u770b\u4f7f\u7528 HashSet \u53bb\u91cd\u7684\u6027\u80fd\uff1a<\/p>\n<p>ps\uff1a\u8fd9\u91cc\u662f\u91c7\u53d6\u4f7f\u7528 set \u7684 add \u65b9\u6cd5\u505a\u53bb\u91cd<\/p>\n<pre><code>\/**\n * \u4f7f\u7528set\u53bb\u91cd\n *\n * @param testList\n *\/\nprivate static void useSetDistinct(List&lt;String&gt; testList) {\n    System.out.println(&quot;HashSet.add \u5f00\u59cb\u53bb\u91cd\uff0c\u6761\u6570\uff1a&quot; + testList.size());\n    List&lt;String&gt; testListDistinctResult = new ArrayList&lt;&gt;(new HashSet(testList));\n    System.out.println(&quot;HashSet.add \u53bb\u91cd\u5b8c\u6bd5\uff0c\u6761\u6570\uff1a&quot; + testListDistinctResult.size());\n}<\/code><\/pre>\n<p>\u6211\u4eec\u8c03\u7528\u4e00\u4e0b\u770b\u770b\u8017\u65f6\uff1a<\/p>\n<pre><code>public static void main(String[] args) {\n    List&lt;String&gt; testList = getTestList();\n    StopWatch stopWatch = new StopWatch();\n    stopWatch.start();\n    useSetDistinct(testList);\n    stopWatch.stop();\n    System.out.println(&quot;\u53bb\u91cd \u6700\u7ec8\u8017\u65f6&quot; + stopWatch.getTotalTimeMillis());\n}<\/code><\/pre>\n<p>\u8017\u65f6\uff1a<\/p>\n<p><img decoding=\"async\" width=\"665\" height=\"137\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9ztV\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u8bc4\u4ef7\uff1a<\/strong>HashSet \u7684\u6548\u7387\uff0c\u6211\u7684\u5efa\u8bae\u662f\u63a8\u8350\u3002<\/p>\n<h2>\u2462<\/h2>\n<p>\u4e3a\u4ec0\u4e48\u8017\u65f6\u5dee\u8ddd\u8fd9\u4e48\u5927\uff1f<\/p>\n<p>\u4e0d\u591a\u8bf4\uff0c\u6211\u4eec\u770b\u6e90\u7801\uff1a<\/p>\n<p>list.contains(o)\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"323\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9ztW\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u91cc\u9762\u7528\u5230\u4e86 index(o) \uff1a<\/p>\n<p><img decoding=\"async\" width=\"668\" height=\"603\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9ztX\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6 \uff1aO(n) n: \u5143\u7d20\u4e2a\u6570<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u770b\u770b set.add(o) \u662f\u600e\u4e48\u6837\u7684 \uff1a<\/p>\n<p><img decoding=\"async\" width=\"523\" height=\"304\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9ztY\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>map\u7684add , \u8001\u751f\u5e38\u8c08\u5c31\u4e0d\u8c08\u4e86\uff0chash\u5b8c \u76f4\u63a5\u585e\u5230\u67d0\u4e2a\u4f4d\u7f6e\uff0c \u65f6\u95f4\u590d\u6742\u5ea6 \uff1aO(1)  \u3002<\/p>\n<p>\u6240\u4ee5 O(n) \u548c O(1) \u8c01\u5feb\u8c01\u6162\uff1f\u663e\u7136\u3002<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"322\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9ztZ\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u662f\uff1aO(1)<\/p>\n<p><img decoding=\"async\" width=\"386\" height=\"90\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9zt0\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>\u2463<\/h2>\n<p>\u90a3\u4e48\u6211\u4eec\u6700\u540e\u518d\u770b\u770b\u522b\u7684\u53bb\u91cd\uff1a<\/p>\n<p>\u53ccfor\u5faa\u73af \uff0cremove\u53bb\u91cd<\/p>\n<pre><code>\/**\n * \u4f7f\u7528\u53ccfor\u5faa\u73af\u53bb\u91cd\n * @param testList\n *\/\nprivate static void use2ForDistinct(List&lt;String&gt; testList) {\n    System.out.println(&quot;list \u53cc\u5faa\u73af \u5f00\u59cb\u53bb\u91cd\uff0c\u6761\u6570\uff1a&quot; + testList.size());\n    for (int i = 0; i &lt; testList.size(); i++) {\n        for (int j = i + 1; j &lt; testList.size(); j++) {\n            if (testList.get(i).equals(testList.get(j))) {\n                testList.remove(j);\n            }\n        }\n    }\n    System.out.println(&quot;list \u53cc\u5faa\u73af  \u53bb\u91cd\u5b8c\u6bd5\uff0c\u6761\u6570\uff1a&quot; + testList.size());\n}\npublic static void main(String[] args) {\n    List&lt;String&gt; testList = getTestList();\n    StopWatch stopWatch = new StopWatch();\n    stopWatch.start();\n    use2ForDistinct(testList);\n    stopWatch.stop();\n    System.out.println(&quot;\u53bb\u91cd \u6700\u7ec8\u8017\u65f6&quot; + stopWatch.getTotalTimeMillis());\n}<\/code><\/pre>\n<p>\u8017\u65f6\uff1a<\/p>\n<p><img decoding=\"async\" width=\"548\" height=\"125\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9zt1\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u8bc4\u4ef7\uff1a<\/strong>\u77e5\u9053\u5c31\u884c\uff0c\u56fe\u4e2a\u4e50\uff0c\u522b\u7528\uff0c\u8d3c\u6162\uff0c\u800c\u4e14\u4ee3\u7801\u770b\u8d77\u6765\u4e71<\/p>\n<p>stream\u7684distinct\u53bb\u91cd\uff1a<\/p>\n<pre><code>\/**\n * \u4f7f\u7528Stream \u53bb\u91cd\n *\n * @param testList\n *\/\nprivate static void useStreamDistinct(List&lt;String&gt; testList) {\n    System.out.println(&quot;stream \u5f00\u59cb\u53bb\u91cd\uff0c\u6761\u6570\uff1a&quot; + testList.size());\n    List&lt;String&gt; testListDistinctResult = testList.stream().distinct().collect(Collectors.toList());\n    System.out.println(&quot;stream \u53bb\u91cd\u5b8c\u6bd5\uff0c\u6761\u6570\uff1a&quot; + testListDistinctResult.size());\n}\npublic static void main(String[] args) {\n    List&lt;String&gt; testList = getTestList();\n    StopWatch stopWatch = new StopWatch();\n    stopWatch.start();\n    useStreamDistinct(testList);\n    stopWatch.stop();\n    System.out.println(&quot;\u53bb\u91cd \u6700\u7ec8\u8017\u65f6&quot; + stopWatch.getTotalTimeMillis());\n}<\/code><\/pre>\n<p>\u8017\u65f6\uff1a<\/p>\n<p><img decoding=\"async\" width=\"481\" height=\"99\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9zt2\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u8bc4\u4ef7\uff1a<\/strong>\u8fd8\u4e0d\u9519\uff0c\u4e3b\u8981\u662f\u4ee3\u7801\u4e5f\u86ee\u7b80\u6d01\uff0c\u6709\u4e00\u70b9\u70b9\u52a8\u5fc3\u3002<\/p>\n<p>\u6765\u6e90\uff1ablog.csdn.net\/qq_35387940\/article\/details\/129885310<\/p>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=QNhNI6%2F0t%2FYymvtX9jKmKg%3D%3D.MUwN5vFhWm6ZJwOdjMlCUFqiHTj8o95phj02jQBG%2Blc%3D\" rel=\"noopener noreferrer\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=cbweeJhxrg2dB36K%2FKLPug%3D%3D.9DSMNRTGvl2RTLpfU7WMahU4d2NUWbHDtOnC9ukhflXrCb%2F9fpYaT6joxERlbdkgLm0rtThqVK47AVNCWlvAqQ%3D%3D\" rel=\"noopener noreferrer\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=1OrO5%2F0YAQDj1Wn17hx7Bg%3D%3D.LDnQTnVx2G7%2BUa12EU0dyNFffdTusoSJ%2FTw5lUyGvHJb7%2FKeCAiOWX6dQJchFRB1\" rel=\"noopener noreferrer\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=ROhS4CRMIty878vQ34FE5Q%3D%3D.vMN%2FdmDV3SZPlfKyGGwsebqUk3lXHARtE0GM%2Fv0H8X%2F9NoqCnXypP%2BYOLpSJ%2By%2BjiR2T%2BxM3fodM1NPUu8e4lg%3D%3D\" rel=\"noopener noreferrer\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=ZthZXZThSo7lhyIVIqyhQQ%3D%3D.OAyO1DScslFWgzezmJJp6ig6CQ1eBjNrLig%2B1KV7WvSCV%2Fy3z0AfsqaXv9woF4WX%2BWNmh76%2FBCtGW2yaUY5lplgKhPLJoR0y%2FIzL6a8DcCc%3D\" rel=\"noopener noreferrer\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044188013\">https:\/\/segmentfault.com\/a\/1190000044188013<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u53c8\u662f\u4e00\u8f6e\u4ee3\u7801review , \u53d1\u73b0\u4e86\u4e00\u4e9b\u5b9e\u73b0\u53bb\u91cd\u7684\u4ee3\u7801\uff0c\u5728\u4f7f\u7528 list.contain &#8230;&#038; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008235","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008235"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008235\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}