{"id":303895,"date":"2024-05-20T19:10:22","date_gmt":"2024-05-20T11:10:22","guid":{"rendered":""},"modified":"2024-05-20T19:10:39","modified_gmt":"2024-05-20T11:10:39","slug":"java-%e7%bc%96%e7%a8%8b%e4%bb%a3%e7%a0%81%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%94%9f%e6%88%90%e5%99%a8%e5%92%8c%e8%bf%ad%e4%bb%a3%e5%99%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/303895.html","title":{"rendered":"java \u7f16\u7a0b\u4ee3\u7801\u4e2d\u5982\u4f55\u5b9e\u73b0\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26220141\/efca059b-608f-4667-a80c-7dbcb7740231.webp\" alt=\"java \u7f16\u7a0b\u4ee3\u7801\u4e2d\u5982\u4f55\u5b9e\u73b0\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\" \/><\/p>\n<p><p>\u5728Java\u7f16\u7a0b\u4e2d\uff0c\u5b9e\u73b0\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\u7684\u8fc7\u7a0b\u6d89\u53ca\u5230\u51e0\u4e2a\u6838\u5fc3\u6982\u5ff5\uff1a<strong>\u8fed\u4ee3\u5668\u6a21\u5f0f\u3001\u751f\u6210\u5668\u6a21\u5f0f\u3001<code>Iterable<\/code>\u63a5\u53e3\u3001<code>Iterator<\/code>\u63a5\u53e3<\/strong>\u3002\u5728Java\u4e2d\uff0c\u8fed\u4ee3\u5668\uff08Iterator\uff09\u6a21\u5f0f\u7528\u4e8e\u4ee5\u987a\u5e8f\u65b9\u5f0f\u8bbf\u95ee\u96c6\u5408\u5bf9\u8c61\u7684\u5143\u7d20\uff0c\u800c\u4e0d\u9700\u8981\u77e5\u6653\u96c6\u5408\u5bf9\u8c61\u7684\u5e95\u5c42\u8868\u793a\u3002\u751f\u6210\u5668\uff08Generator\uff09\u5728Java\u4e2d\u4e0d\u662f\u4e00\u4e2a\u539f\u751f\u6982\u5ff5\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5668\u6a21\u5f0f\u5b9e\u73b0\u7c7b\u4f3c\u529f\u80fd\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u61d2\u52a0\u8f7d\u6216\u9010\u6b65\u751f\u6210\u5143\u7d20\u65f6\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u8fed\u4ee3\u5668\u6a21\u5f0f\u4e0e<code>Iterator<\/code>\u63a5\u53e3<\/strong><\/p>\n<\/p>\n<p><p>\u8fed\u4ee3\u5668\u6a21\u5f0f\u662f\u8bbe\u8ba1\u6a21\u5f0f\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6cd5\u987a\u5e8f\u8bbf\u95ee\u4e00\u4e2a\u805a\u5408\u5bf9\u8c61\u4e2d\u5404\u4e2a\u5143\u7d20\uff0c\u800c\u53c8\u4e0d\u9700\u8981\u66b4\u9732\u8be5\u5bf9\u8c61\u7684\u5185\u90e8\u8868\u793a\u3002\u5728Java\u4e2d\uff0c\u8fed\u4ee3\u5668\u6a21\u5f0f\u901a\u8fc7<code>Iterator<\/code>\u63a5\u53e3\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u8fed\u4ee3\u5668<\/strong><\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u8fed\u4ee3\u5668\uff0c\u9996\u5148\u9700\u8981\u5b9e\u73b0<code>Iterator<\/code>\u63a5\u53e3\u3002\u8fd9\u6d89\u53ca\u5230\u5b9e\u73b0<code>hasNext()<\/code>\u548c<code>next()<\/code>\u4e24\u4e2a\u65b9\u6cd5\u3002<code>hasNext()<\/code>\u7528\u4e8e\u68c0\u6d4b\u5e8f\u5217\u4e2d\u662f\u5426\u8fd8\u6709\u5143\u7d20\uff0c<code>next()<\/code>\u7528\u4e8e\u8fd4\u56de\u5e8f\u5217\u4e2d\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a<code>NumberRange<\/code>\u7684\u7c7b\uff0c\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u6570\u5b57\u8303\u56f4\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4e3a\u5b83\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><pre><code class=\"language-java\">class NumberRangeIterator implements Iterator&lt;Integer&gt; {<\/p>\n<p>    private int start;<\/p>\n<p>    private int end;<\/p>\n<p>    private int current;<\/p>\n<p>    public NumberRangeIterator(int start, int end) {<\/p>\n<p>        this.start = start;<\/p>\n<p>        this.end = end;<\/p>\n<p>        this.current = start;<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    public boolean hasNext() {<\/p>\n<p>        return current &lt;= end;<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    public Integer next() {<\/p>\n<p>        if (!hasNext()) {<\/p>\n<p>            throw new NoSuchElementException();<\/p>\n<p>        }<\/p>\n<p>        return current++;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u751f\u6210\u5668\u6a21\u5f0f\u4e0e\u60f0\u6027\u6c42\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u6a21\u5f0f\u662f\u4e00\u79cd\u521b\u5efa\u5bf9\u8c61\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u65e8\u5728\u627e\u5230\u4e00\u79cd\u65b9\u6cd5\u5c06\u5bf9\u8c61\u7684\u6784\u5efa\u4e0e\u8868\u793a\u5206\u79bb\uff0c\u4ee5\u4fbf\u540c\u4e00\u4e2a\u6784\u5efa\u8fc7\u7a0b\u53ef\u4ee5\u521b\u5efa\u4e0d\u540c\u7684\u8868\u793a\u3002\u5728Java\u4e2d\uff0c\u751f\u6210\u5668\u6a21\u5f0f\u901a\u5e38\u7528\u4e8e\u6784\u5efa\u590d\u6742\u5bf9\u8c61\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u501f\u9274\u5176\u601d\u60f3\u6765\u5b9e\u73b0\u7c7b\u4f3cPython\u4e2d\u7684\u751f\u6210\u5668\u6982\u5ff5\uff0c\u7279\u522b\u662f\u7528\u4e8e\u60f0\u6027\u6c42\u503c\uff08\u61d2\u52a0\u8f7d\uff09\u573a\u666f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b9e\u73b0\u60f0\u6027\u6c42\u503c\u7684\u8fed\u4ee3\u5668<\/strong><\/p>\n<p>\u867d\u7136Java\u6ca1\u6709\u539f\u751f\u7684\u751f\u6210\u5668\u652f\u6301\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5668\u63a5\u53e3\u6a21\u62df\u751f\u6210\u5668\u7684\u884c\u4e3a\uff0c\u5b9e\u73b0\u60f0\u6027\u6c42\u503c\u3002\u4ee5\u4e0b\u5c55\u793a\u5982\u4f55\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u65e0\u9650\u6574\u6570\u5e8f\u5217\u751f\u6210\u5668\uff1a<\/li>\n<\/p>\n<\/ul>\n<p><pre><code class=\"language-java\">class InfiniteIntegerGenerator implements Iterator&lt;Integer&gt; {<\/p>\n<p>    private int current = 0;<\/p>\n<p>    @Override<\/p>\n<p>    public boolean hasNext() {<\/p>\n<p>        return true; \/\/ \u65e0\u9650\u5e8f\u5217\u59cb\u7ec8\u6709\u4e0b\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    public Integer next() {<\/p>\n<p>        return current++; \/\/ \u8fd4\u56de\u5f53\u524d\u503c\u540e\u9012\u589e<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n<p>\u6b64\u6a21\u5f0f\u975e\u5e38\u9002\u7528\u4e8e\u9700\u8981\u9010\u6b65\u751f\u6210\u590d\u6742\u5e8f\u5217\u6216\u6570\u636e\u6d41\u7684\u60c5\u51b5\uff0c\u5c24\u5176\u662f\u5f53\u5b8c\u6574\u5e8f\u5217\u4e00\u6b21\u6027\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u4e0d\u5b9e\u9645\u6216\u8d44\u6e90\u6d88\u8017\u592a\u5927\u65f6\u3002<\/li>\n<\/p>\n<\/ul>\n<p><p><strong>\u4e09\u3001<code>Iterable<\/code>\u63a5\u53e3\u4e0e<code>for-each<\/code>\u5faa\u73af<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u80fd\u591f\u4f7f\u7528Java\u7684<code>for-each<\/code>\u5faa\u73af\u6765\u904d\u5386\u81ea\u5b9a\u4e49\u7684\u96c6\u5408\uff0c\u8be5\u96c6\u5408\u7c7b\u9700\u8981\u5b9e\u73b0<code>Iterable<\/code>\u63a5\u53e3\u3002\u8be5\u63a5\u53e3\u8981\u6c42\u5b9e\u73b0\u4e00\u4e2a\u8fd4\u56de<code>Iterator<\/code>\u5bf9\u8c61\u7684<code>iterator()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b9e\u73b0<code>Iterable<\/code>\u63a5\u53e3<\/strong><\/p>\n<p>\u7ee7\u7eed\u4f7f\u7528<code>NumberRange<\/code>\u7c7b\u7684\u4f8b\u5b50\uff0c\u8ba9\u5b83\u5b9e\u73b0<code>Iterable<\/code>\u63a5\u53e3\uff1a<\/li>\n<\/p>\n<\/ul>\n<p><pre><code class=\"language-java\">class NumberRange implements Iterable&lt;Integer&gt; {<\/p>\n<p>    private int start, end;<\/p>\n<p>    public NumberRange(int start, int end) {<\/p>\n<p>        this.start = start;<\/p>\n<p>        this.end = end;<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    public Iterator&lt;Integer&gt; iterator() {<\/p>\n<p>        return new NumberRangeIterator(start, end);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u5229\u7528<code>for-each<\/code>\u5faa\u73af\u8fdb\u884c\u904d\u5386<\/strong><\/p>\n<p>\u4e00\u65e6<code>NumberRange<\/code>\u5b9e\u73b0\u4e86<code>Iterable<\/code>\u63a5\u53e3\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528<code>for-each<\/code>\u5faa\u73af\u6765\u904d\u5386\u8303\u56f4\u5185\u7684\u6570\u5b57\u4e86\uff0c\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u4ee3\u7801\uff1a<\/li>\n<\/p>\n<\/ul>\n<p><pre><code class=\"language-java\">NumberRange range = new NumberRange(1, 5);<\/p>\n<p>for(int num : range) {<\/p>\n<p>    System.out.println(num);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>Iterable<\/code>\u548c<code>Iterator<\/code>\u63a5\u53e3\uff0cJava\u5141\u8bb8\u5f00\u53d1\u8005\u4ee5\u4e00\u79cd\u7b80\u6d01\u800c\u7edf\u4e00\u7684\u65b9\u5f0f\u904d\u5386\u5404\u79cd\u6570\u636e\u7ed3\u6784\u3002\u867d\u7136Java\u4e2d\u4e0d\u5b58\u5728\u50cfPython\u90a3\u6837\u7684\u539f\u751f\u751f\u6210\u5668\u8bed\u6cd5\uff0c\u4f46\u662f\u901a\u8fc7\u8fed\u4ee3\u5668\u6a21\u5f0f\uff0c\u5f00\u53d1\u8005\u4f9d\u7136\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u751f\u6210\u5668\u7684\u884c\u4e3a\uff0c\u5c24\u5176\u662f\u5728\u8fdb\u884c\u60f0\u6027\u6c42\u503c\u6216\u9010\u6b65\u751f\u6210\u590d\u6742\u6570\u636e\u5e8f\u5217\u65f6\u3002\u8fd9\u4e9b\u6280\u672f\u624b\u6bb5\u4e3a\u5904\u7406\u5927\u91cf\u6570\u636e\u3001\u6784\u5efa\u590d\u6742\u5bf9\u8c61\u63d0\u4f9b\u4e86\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728Java\u7f16\u7a0b\u4e2d\u5b9e\u73b0\u751f\u6210\u5668\uff1f<\/strong><\/p>\n<p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u53ef\u4ee5\u6309\u9700\u751f\u6210\u5bf9\u8c61\u5e8f\u5217\u7684\u673a\u5236\uff0c\u5b83\u5e38\u88ab\u7528\u4e8e\u5904\u7406\u5927\u6570\u636e\u96c6\u6216\u9700\u8981\u9010\u6b65\u52a0\u8f7d\u6570\u636e\u7684\u60c5\u51b5\u3002\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u751f\u6210\u5668\u3002<\/p>\n<p>\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u7c7b\uff0c\u8be5\u7c7b\u5b9e\u73b0\u4e86<code>Iterable<\/code>\u63a5\u53e3\uff0c\u5e76\u91cd\u5199<code>Iterator<\/code>\u65b9\u6cd5\u3002\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e9b\u6570\u636e\u7ed3\u6784\u6216\u7b97\u6cd5\uff0c\u7528\u4e8e\u751f\u6210\u9700\u8981\u7684\u5bf9\u8c61\u5e8f\u5217\u3002\u5728<code>Iterator<\/code>\u65b9\u6cd5\u4e2d\uff0c\u4f60\u53ef\u4ee5\u63a7\u5236\u4f55\u65f6\u751f\u6210\u4e0b\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u65b9\u6cd5\u8fd4\u56de\u8fd9\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>\u9664\u4e86\u4f7f\u7528\u63a5\u53e3<code>Iterable<\/code>\u548c<code>Iterator<\/code>\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528Java 8\u5f15\u5165\u7684Stream API\u6765\u5b9e\u73b0\u751f\u6210\u5668\u3002Stream API\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5f3a\u5927\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u5de5\u5177\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u751f\u6210\u5bf9\u8c61\u5e8f\u5217\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>Stream.generate<\/code>\u65b9\u6cd5\uff0c\u4f20\u5165\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\uff0c\u6765\u521b\u5efa\u4e00\u4e2a\u751f\u6210\u5668\u6d41\u3002<\/p>\n<p>\u603b\u4e4b\uff0c\u65e0\u8bba\u662f\u901a\u8fc7\u7f16\u5199\u7c7b\u8fd8\u662f\u4f7f\u7528Stream API\uff0c\u4f60\u90fd\u53ef\u4ee5\u5f88\u8f7b\u677e\u5730\u5728Java\u7f16\u7a0b\u4e2d\u5b9e\u73b0\u751f\u6210\u5668\u529f\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Java\u7f16\u7a0b\u4e2d\u5b9e\u73b0\u8fed\u4ee3\u5668\uff1f<\/strong><\/p>\n<p>\u8fed\u4ee3\u5668\u5141\u8bb8\u6211\u4eec\u5bf9\u4e00\u4e2a\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u987a\u5e8f\u8bbf\u95ee\uff0c\u800c\u4e0d\u5fc5\u5173\u5fc3\u5bb9\u5668\u7684\u5e95\u5c42\u5b9e\u73b0\u3002\u5728Java\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>Iterator<\/code>\u63a5\u53e3\u6765\u5b9e\u73b0\u8fed\u4ee3\u5668\u3002<\/p>\n<p>\u8981\u5b9e\u73b0\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u4f60\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u7c7b\uff0c\u8be5\u7c7b\u5b9e\u73b0\u4e86<code>Iterator<\/code>\u63a5\u53e3\uff0c\u5e76\u5b9a\u4e49\u4e86\u6240\u9700\u7684\u65b9\u6cd5\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec<code>hasNext()<\/code>\u7528\u4e8e\u68c0\u67e5\u662f\u5426\u8fd8\u6709\u4e0b\u4e00\u4e2a\u5143\u7d20\uff0c<code>next()<\/code>\u7528\u4e8e\u8fd4\u56de\u4e0b\u4e00\u4e2a\u5143\u7d20\uff0c<code>remove()<\/code>\u7528\u4e8e\u5220\u9664\u96c6\u5408\u4e2d\u7684\u5f53\u524d\u5143\u7d20\u3002<\/p>\n<p>\u5728\u5b9e\u73b0<code>Iterator<\/code>\u63a5\u53e3\u65f6\uff0c\u4f60\u9700\u8981\u7ba1\u7406\u8fed\u4ee3\u5668\u7684\u72b6\u6001\u5e76\u786e\u4fdd\u6b63\u786e\u7684\u8fed\u4ee3\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u904d\u5386\u4e00\u4e2a\u6570\u7ec4\uff0c\u4f60\u9700\u8981\u8ddf\u8e2a\u5f53\u524d\u7684\u7d22\u5f15\uff0c\u4ee5\u4fbf\u5728\u6bcf\u6b21\u8c03\u7528<code>next()<\/code>\u65b9\u6cd5\u65f6\u8fd4\u56de\u6b63\u786e\u7684\u5143\u7d20\u3002<\/p>\n<p>\u9664\u4e86\u624b\u52a8\u5b9e\u73b0\u8fed\u4ee3\u5668\uff0cJava\u4e2d\u7684\u5404\u79cd\u96c6\u5408\u7c7b\u90fd\u5df2\u7ecf\u5185\u7f6e\u4e86\u8fed\u4ee3\u5668\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u96c6\u5408\u7c7b\u7684<code>iterator()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u8fd9\u4e2a\u8fed\u4ee3\u5668\u6765\u904d\u5386\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Java\u7f16\u7a0b\u4e2d\u540c\u65f6\u5b9e\u73b0\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\uff1f<\/strong><\/p>\n<p>\u5728Java\u7f16\u7a0b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u540c\u65f6\u5b9e\u73b0\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\uff0c\u4ee5\u4fbf\u80fd\u591f\u6309\u9700\u751f\u6210\u5bf9\u8c61\u5e8f\u5217\u5e76\u5bf9\u5176\u8fdb\u884c\u987a\u5e8f\u8bbf\u95ee\u3002<\/p>\n<p>\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u8be5\u7c7b\u65e2\u5b9e\u73b0\u4e86<code>Iterable<\/code>\u63a5\u53e3\uff0c\u4e5f\u5b9e\u73b0\u4e86<code>Iterator<\/code>\u63a5\u53e3\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u4e2a\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e9b\u6570\u636e\u7ed3\u6784\u6216\u7b97\u6cd5\uff0c\u7528\u4e8e\u751f\u6210\u5bf9\u8c61\u5e8f\u5217\u3002\u5728<code>Iterator<\/code>\u65b9\u6cd5\u4e2d\uff0c\u4f60\u53ef\u4ee5\u63a7\u5236\u4f55\u65f6\u751f\u6210\u4e0b\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u65b9\u6cd5\u8fd4\u56de\u8fd9\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>\u540c\u6837\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528Java 8\u5f15\u5165\u7684Stream API\u6765\u5b9e\u73b0\u540c\u65f6\u5177\u5907\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\u529f\u80fd\u7684\u4ee3\u7801\u3002\u901a\u8fc7\u4f7f\u7528<code>Stream<\/code>\u7684<code>generate<\/code>\u65b9\u6cd5\uff0c\u5e76\u914d\u5408<code>Iterator<\/code>\u65b9\u6cd5\u6765\u9650\u5236\u751f\u6210\u5668\u7684\u6570\u91cf\uff0c\u4f60\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u751f\u6210\u5e76\u987a\u5e8f\u8bbf\u95ee\u5bf9\u8c61\u5e8f\u5217\u3002<\/p>\n<p>\u65e0\u8bba\u662f\u901a\u8fc7\u7f16\u5199\u7c7b\u8fd8\u662f\u4f7f\u7528Stream API\uff0c\u4f60\u90fd\u53ef\u4ee5\u5728Java\u7f16\u7a0b\u4e2d\u540c\u65f6\u5b9e\u73b0\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\u7684\u529f\u80fd\u3002\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u6839\u636e\u9700\u8981\u751f\u6210\u5bf9\u8c61\uff0c\u7136\u540e\u6309\u7167\u7279\u5b9a\u7684\u987a\u5e8f\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Java\u7f16\u7a0b\u4e2d\uff0c\u5b9e\u73b0\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\u7684\u8fc7\u7a0b\u6d89\u53ca\u5230\u51e0\u4e2a\u6838\u5fc3\u6982\u5ff5\uff1a\u8fed\u4ee3\u5668\u6a21\u5f0f\u3001\u751f\u6210\u5668\u6a21\u5f0f\u3001Iterable\u63a5\u53e3\u3001I [&hellip;]","protected":false},"author":3,"featured_media":303920,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/303895"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=303895"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/303895\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/303920"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=303895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=303895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=303895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}