{"id":67020,"date":"2025-05-03T13:48:40","date_gmt":"2025-05-03T05:48:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/67020\/"},"modified":"2025-05-03T13:48:40","modified_gmt":"2025-05-03T05:48:40","slug":"java%e4%b8%a4%e4%b8%aa%e6%95%b4%e6%95%b0%e6%95%b0%e7%bb%84%e7%9a%84%e6%80%bb%e5%92%8c%e6%80%8e%e4%b9%88%e7%ae%97-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/67020\/","title":{"rendered":"java\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\u7684\u603b\u548c\u600e\u4e48\u7b97"},"content":{"rendered":"<blockquote><p>\n  \u8ba1\u7b97\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\u7684\u603b\u548c\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u904d\u5386\u6c42\u548c\u548c\u4f7f\u7528\u6d41\u548c\u5e76\u884c\u6d41\u3002\u904d\u5386\u6c42\u548c\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u9010\u4e2a\u904d\u5386\u4e24\u4e2a\u6570\u7ec4\uff0c\u5c06\u5bf9\u5e94\u5143\u7d20\u76f8\u52a0\u5b58\u50a8\u5728\u65b0\u6570\u7ec4\u4e2d\u3002\u4f7f\u7528\u6d41\u548c\u5e76\u884c\u6d41\u6cd5\u4f7f\u7528 intstream \u521b\u5efa\u4e24\u4e2a\u6570\u7ec4\u7684\u6d41\uff0c\u8c03\u7528 sum() \u65b9\u6cd5\u8ba1\u7b97\u6d41\u4e2d\u7684\u5143\u7d20\u603b\u548c\uff0c\u5e76\u4f7f\u7528 parallel() \u65b9\u6cd5\u5e76\u884c\u5904\u7406\u6d41\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/18\/2024111814122924313.jpg\" class=\"aligncenter\" title=\"java\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\u7684\u603b\u548c\u600e\u4e48\u7b97\u63d2\u56fe\" alt=\"java\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\u7684\u603b\u548c\u600e\u4e48\u7b97\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8ba1\u7b97\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\u7684\u603b\u548c\uff1f<\/strong><\/p>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u6709\u4e24\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\u7684\u603b\u548c\uff1a<\/p>\n<p><strong>1. \u904d\u5386\u6c42\u548c<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u957f\u5ea6\u7b49\u4e8e\u4e24\u4e2a\u8f93\u5165\u6570\u7ec4\u6700\u957f\u8005\u7684\u957f\u5ea6\u3002<\/li>\n<li>\u9010\u4e2a\u904d\u5386\u4e24\u4e2a\u8f93\u5165\u6570\u7ec4\uff0c\u5c06\u5bf9\u5e94\u7684\u5143\u7d20\u76f8\u52a0\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u65b0\u6570\u7ec4\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public static int[] sumArrays(int[] arr1, int[] arr2) {\n    int[] result = new int[Math.max(arr1.length, arr2.length)];\n    for (int i = 0; i &lt; result.length; i++) {\n        result[i] = (i &lt; arr1.length ? arr1[i] : 0) + (i &lt; arr2.length ? arr2[i] : 0);\n    }\n    return result;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u6d41\u548c\u5e76\u884c\u6d41<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 IntStream \u521b\u5efa\u4e24\u4e2a\u8f93\u5165\u6570\u7ec4\u7684\u6d41\u3002<\/li>\n<li>\u8c03\u7528 sum() \u65b9\u6cd5\u8ba1\u7b97\u6d41\u4e2d\u7684\u5143\u7d20\u603b\u548c\u3002<\/li>\n<li>\u4f7f\u7528 parallel() \u65b9\u6cd5\u5e76\u884c\u5904\u7406\u6d41\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public static int sumArrays(int[] arr1, int[] arr2) {\n    return IntStream.of(arr1).sum() + IntStream.of(arr2).sum();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5e76\u884c\u6d41\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public static int sumArraysParallel(int[] arr1, int[] arr2) {\n    return IntStream.of(arr1).parallel().sum() + IntStream.of(arr2).parallel().sum();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\u7684\u603b\u548c\u600e\u4e48\u7b97\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>\u8ba1\u7b97\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\u7684\u603b\u548c\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u904d\u5386\u6c42\u548c\u548c\u4f7f\u7528\u6d41\u548c\u5e76\u884c\u6d41\u3002\u904d\u5386\u6c42\u548c\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u9010\u4e2a\u904d\u5386\u4e24\u4e2a\u6570\u7ec4\uff0c\u5c06\u5bf9\u5e94\u5143\u7d20\u76f8\u52a0\u5b58\u50a8\u5728\u65b0\u6570\u7ec4\u4e2d\u3002\u4f7f\u7528\u6d41\u548c\u5e76\u884c\u6d41\u6cd5\u4f7f\u7528 intstream \u521b\u5efa\u4e24\u4e2a\u6570\u7ec4\u7684\u6d41\uff0c\u8c03\u7528 sum() \u65b9\u6cd5\u8ba1\u7b97\u6d41\u4e2d\u7684\u5143\u7d20\u603b\u548c\uff0c\u5e76\u4f7f\u7528 parallel() \u65b9\u6cd5\u5e76\u884c\u5904\u7406\u6d41\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002 \u5982\u4f55\u8ba1\u7b97\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\u7684\u603b\u548c\uff1f \u65b9\u6cd5\uff1a \u6709\u4e24\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\u7684\u603b\u548c\uff1a 1. \u904d\u5386\u6c42\u548c \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u957f\u5ea6\u7b49\u4e8e\u4e24\u4e2a\u8f93\u5165\u6570\u7ec4\u6700\u957f\u8005\u7684\u957f\u5ea6\u3002 \u9010\u4e2a\u904d\u5386\u4e24\u4e2a\u8f93\u5165\u6570\u7ec4\uff0c\u5c06\u5bf9\u5e94\u7684\u5143\u7d20\u76f8\u52a0\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u65b0\u6570\u7ec4\u4e2d\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a public static int[] sumArrays(int[] arr1, int[] arr2) { int[] result = new int[Math.max(arr1.length, arr2.length)]; for (int i = 0; i &lt; result.length; i++) { result[i] = (i &lt; arr1.length ? arr1[i] : 0) + (i &lt; arr2.length ? arr2[i] [&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-67020","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/67020","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=67020"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/67020\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=67020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=67020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=67020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}