{"id":37649,"date":"2024-11-26T15:28:23","date_gmt":"2024-11-26T07:28:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37649\/"},"modified":"2024-11-26T15:28:23","modified_gmt":"2024-11-26T07:28:23","slug":"java%e6%80%8e%e4%b9%88%e8%ae%b2%e9%9b%86%e5%90%88%e5%8f%98%e4%b8%ba%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37649\/","title":{"rendered":"java\u600e\u4e48\u8bb2\u96c6\u5408\u53d8\u4e3a\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7 java 8 \u6d41\u5f0f api \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u521b\u5efa\u6d41\uff1a\u901a\u8fc7\u96c6\u5408\u7684 stream() \u65b9\u6cd5\u521b\u5efa\u6d41\u3002\u6620\u5c04\u5230\u6570\u7ec4\uff1a\u4f7f\u7528 toarray() \u65b9\u6cd5\u5c06\u6d41\u6620\u5c04\u5230\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111308211739890.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u8bb2\u96c6\u5408\u53d8\u4e3a\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u8bb2\u96c6\u5408\u53d8\u4e3a\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u901a\u8fc7\u6d41\u5f0f API \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/p>\n<p>\u96c6\u5408\u7c7b\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5c06\u5176\u5143\u7d20\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7 Java 8 \u6d41\u5f0f API\u3002<\/p>\n<p><strong>\u8f6c\u6362\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u521b\u5efa\u6d41\uff1a<\/strong>\u901a\u8fc7\u96c6\u5408\u7684 stream() \u65b9\u6cd5\u521b\u5efa\u6d41\u3002<\/li>\n<li> <strong>\u6620\u5c04\u5230\u6570\u7ec4\uff1a<\/strong>\u4f7f\u7528 toArray() \u65b9\u6cd5\u5c06\u6d41\u6620\u5c04\u5230\u6570\u7ec4\u3002<\/li>\n<\/ol>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u96c6\u5408\nSet&lt;String&gt; names = new HashSet&lt;&gt;();\nnames.add(\"John\");\nnames.add(\"Mary\");\nnames.add(\"Bob\");\n\n\/\/ \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\nString[] namesArray = names.stream()\n                           .toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>toArray() \u65b9\u6cd5\u4f7f\u7528\u4e00\u4e2a\u5de5\u5382\u65b9\u6cd5\u6765\u521b\u5efa\u76ee\u6807\u6570\u7ec4\u3002\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\uff08\u5982 int[]\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 toArray(int[]::new)\u3002<\/li>\n<li>\u8fd8\u53ef\u4ee5\u6307\u5b9a\u6570\u7ec4\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\uff1atoArray(new String[0]) \u4f1a\u521b\u5efa\u4e00\u4e2a\u7a7a\u3002<\/li>\n<li>\u5bf9\u4e8e\u5e76\u53d1\u96c6\u5408\uff0c\u5982 ConcurrentHashMap\uff0c\u53ef\u4ee5\u4f7f\u7528 toArray(int) \u65b9\u6cd5\u6307\u5b9a\u6570\u7ec4\u7684\u5bb9\u91cf\u3002<\/li>\n<\/ul>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u9664\u4e86\u6d41\u5f0f API \u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a<\/p>\n<ul>\n<li> <strong>\u4f7f\u7528 List.toArray() \u65b9\u6cd5\uff1a<\/strong>\u4ec5\u9002\u7528\u4e8e List \u63a5\u53e3\u5b9e\u73b0\u3002<\/li>\n<li> <strong>\u4f7f\u7528 Collection.toArray(T[]) \u65b9\u6cd5\uff1a<\/strong>\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u6570\u7ec4\u7c7b\u578b\u3002<\/li>\n<li> <strong>\u4f7f\u7528 Arrays.asList() \u65b9\u6cd5\uff1a<\/strong>\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a List\uff0c\u7136\u540e\u4f7f\u7528 toArray() \u65b9\u6cd5\u3002<\/li>\n<li> <strong>\u4f7f\u7528 loop \u904d\u5386\u96c6\u5408\u5e76\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u6570\u7ec4\uff1a<\/strong>\u8fd9\u662f\u8f83\u4f4e\u6548\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u8bb2\u96c6\u5408\u53d8\u4e3a\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7 java 8 \u6d41\u5f0f api \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u521b\u5efa\u6d41\uff1a\u901a\u8fc7\u96c6\u5408\u7684 stream() \u65b9\u6cd5\u521b\u5efa\u6d41\u3002\u6620\u5c04\u5230\u6570\u7ec4\uff1a\u4f7f\u7528 toarray() \u65b9\u6cd5\u5c06\u6d41\u6620\u5c04\u5230\u6570\u7ec4\u3002 \u901a\u8fc7\u6d41\u5f0f API \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4 \u96c6\u5408\u7c7b\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5c06\u5176\u5143\u7d20\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7 Java 8 \u6d41\u5f0f API\u3002 \u8f6c\u6362\u6b65\u9aa4\uff1a \u521b\u5efa\u6d41\uff1a\u901a\u8fc7\u96c6\u5408\u7684 stream() \u65b9\u6cd5\u521b\u5efa\u6d41\u3002 \u6620\u5c04\u5230\u6570\u7ec4\uff1a\u4f7f\u7528 toArray() \u65b9\u6cd5\u5c06\u6d41\u6620\u5c04\u5230\u6570\u7ec4\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u96c6\u5408 Set&lt;String&gt; names = new HashSet&lt;&gt;(); names.add(&#8220;John&#8221;); names.add(&#8220;Mary&#8221;); names.add(&#8220;Bob&#8221;); \/\/ \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4 String[] namesArray = names.stream() .toArray(String[]::new); \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\uff1a toArray() \u65b9\u6cd5\u4f7f\u7528\u4e00\u4e2a\u5de5\u5382\u65b9\u6cd5\u6765\u521b\u5efa\u76ee\u6807\u6570\u7ec4\u3002\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\uff08\u5982 int[]\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 toArray(int[]::new)\u3002 \u8fd8\u53ef\u4ee5\u6307\u5b9a\u6570\u7ec4\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\uff1atoArray(new String[0]) \u4f1a\u521b\u5efa\u4e00\u4e2a\u7a7a\u3002 \u5bf9\u4e8e\u5e76\u53d1\u96c6\u5408\uff0c\u5982 ConcurrentHashMap\uff0c\u53ef\u4ee5\u4f7f\u7528 [&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-37649","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37649","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=37649"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37649\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}