{"id":65297,"date":"2025-05-03T17:15:35","date_gmt":"2025-05-03T09:15:35","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65297\/"},"modified":"2025-05-03T17:15:35","modified_gmt":"2025-05-03T09:15:35","slug":"java%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8f%98%e6%88%90set-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65297\/","title":{"rendered":"java\u6570\u7ec4\u600e\u4e48\u53d8\u6210set"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7\u4f7f\u7528 arrays.aslist(array) \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a list\uff0c\u518d\u901a\u8fc7 set \u6784\u9020\u51fd\u6570\u521b\u5efa set\uff0c\u6216\u4f7f\u7528 streams \u7684 collectors.toset() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06 java \u6570\u7ec4\u8f6c\u6362\u4e3a set\u3002\u5982\u679c\u8981\u4fdd\u7559\u91cd\u590d\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 linkedhashset \u800c\u4e0d\u662f hashset\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111219094731137.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u600e\u4e48\u53d8\u6210set\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u600e\u4e48\u53d8\u6210set\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u6570\u7ec4\u8f6c\u6362\u4e3a Set<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u65b9\u6cd5<\/strong><\/p>\n<p>\u4f7f\u7528 Arrays.asList(array) \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a List\uff0c\u7136\u540e\u4f7f\u7528 Set \u7684\u6784\u9020\u51fd\u6570\u521b\u5efa\u4e00\u4e2a Set\uff1a<\/p>\n<pre>int[] myArray = {1, 2, 3, 4, 5};\nSet&lt;Integer&gt; mySet = new HashSet&lt;&gt;(Arrays.asList(myArray));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Streams<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4f7f\u7528 Streams \u7684 Collectors.toSet() \u65b9\u6cd5\uff1a<\/p>\n<pre>int[] myArray = {1, 2, 3, 4, 5};\nSet&lt;Integer&gt; mySet = Arrays.stream(myArray).boxed().collect(Collectors.toSet());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4fdd\u7559\u91cd\u590d\u503c<\/strong><\/p>\n<p>\u8981\u4fdd\u7559\u91cd\u590d\u503c\uff0c\u8bf7\u4f7f\u7528 LinkedHashSet \u800c\u4e0d\u662f HashSet\uff1a<\/p>\n<pre>int[] myArray = {1, 2, 2, 3, 4, 5};\nSet&lt;Integer&gt; mySet = new LinkedHashSet&lt;&gt;(Arrays.asList(myArray));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u5c06\u6570\u7ec4\u4e2d\u7684\u539f\u59cb\u503c\u8f6c\u6362\u4e3a Set \u4e2d\u7684\u5305\u88c5\u7c7b\u5bf9\u8c61\uff08\u4f8b\u5982 Integer\uff09\u3002<\/li>\n<li>\u5982\u679c\u6570\u7ec4\u5305\u542b null \u503c\uff0c\u53ef\u80fd\u4f1a\u5f15\u53d1 NullPointerException\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u53d8\u6210set\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u4f7f\u7528 arrays.aslist(array) \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a list\uff0c\u518d\u901a\u8fc7 set \u6784\u9020\u51fd\u6570\u521b\u5efa set\uff0c\u6216\u4f7f\u7528 streams \u7684 collectors.toset() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06 java \u6570\u7ec4\u8f6c\u6362\u4e3a set\u3002\u5982\u679c\u8981\u4fdd\u7559\u91cd\u590d\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 linkedhashset \u800c\u4e0d\u662f hashset\u3002 \u5982\u4f55\u5c06 Java \u6570\u7ec4\u8f6c\u6362\u4e3a Set \u76f4\u63a5\u65b9\u6cd5 \u4f7f\u7528 Arrays.asList(array) \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a List\uff0c\u7136\u540e\u4f7f\u7528 Set \u7684\u6784\u9020\u51fd\u6570\u521b\u5efa\u4e00\u4e2a Set\uff1a int[] myArray = {1, 2, 3, 4, 5}; Set&lt;Integer&gt; mySet = new HashSet&lt;&gt;(Arrays.asList(myArray)); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 Streams \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4f7f\u7528 Streams \u7684 Collectors.toSet() \u65b9\u6cd5\uff1a int[] myArray = {1, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-65297","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=65297"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65297\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}