{"id":467,"date":"2013-09-14T16:26:02","date_gmt":"2013-09-14T08:26:02","guid":{"rendered":"http:\/\/coderbee.net\/?p=467"},"modified":"2019-01-06T21:25:22","modified_gmt":"2019-01-06T13:25:22","slug":"java-8-%e6%96%b0%e7%89%b9%e6%80%a7%e4%b9%8b-%e6%8e%a5%e5%8f%a3%e6%94%b9%e8%bf%9b%e5%92%8clambdas","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20130914\/467","title":{"rendered":"Java 8 \u65b0\u7279\u6027\u4e4b \u63a5\u53e3\u6539\u8fdb\u548cLambdas"},"content":{"rendered":"<p>\u53c2\u8003\u81ea\uff1a<a href=\"http:\/\/www.techempower.com\/blog\/2013\/03\/26\/everything-about-java-8\/\">http:\/\/www.techempower.com\/blog\/2013\/03\/26\/everything-about-java-8\/<\/a><\/p>\n<h2>\u6d4b\u8bd5\u73af\u5883<\/h2>\n<pre><code>$ java -version\r\njava version \"1.8.0-ea\"\r\nJava(TM) SE Runtime Environment (build 1.8.0-ea-b106)\r\nJava HotSpot(TM) 64-Bit Server VM (build 25.0-b48, mixed mode)\r\n<\/code><\/pre>\n<p>IntelliJ IDEA 12.1.4<br \/>\n<!--more--><\/p>\n<h2>\u63a5\u53e3\u6539\u8fdb<\/h2>\n<p>\u4ee5\u524dJava\u7684\u63a5\u53e3\u91cc\u53ea\u80fd\u58f0\u660e\u65b9\u6cd5\u548c\u5b9a\u4e49\u5e38\u91cf\uff0c\u73b0\u5728\u53ef\u4ee5\u5728\u63a5\u53e3\u91cc\u5b9a\u4e49\u9759\u6001\u65b9\u6cd5\u548c\u9ed8\u8ba4\u65b9\u6cd5\u3002<\/p>\n<h3>\u5b9a\u4e49\u9759\u6001\u65b9\u6cd5<\/h3>\n<p>\u5b9a\u4e49\u9759\u6001\uff08static\uff09\u65b9\u6cd5\u5e26\u6765\u7684\u597d\u5904\u5c31\u662f\u53ef\u4ee5\u51cf\u5c11\u521b\u5efa\u5de5\u5177\u7c7b\u7684\u9700\u6c42\u4e86\u3002\u6bd4\u5982 <code>java.util.Collection<\/code> \u63a5\u53e3\u5b9a\u4e49\u4e86\u4e00\u4e2a\u96c6\u5408\uff0c\u5bf9\u4e8e\u6b64\u63a5\u53e3\u5b9e\u4f8b\u64cd\u4f5c\u7684\u5f88\u591a\u901a\u7528\u65b9\u6cd5\u90fd\u662f\u901a\u8fc7\u5de5\u5177\u7c7b <code>java.util.Collections<\/code> \u6765\u63d0\u4f9b\u7684\uff0c\u73b0\u5728\u53ef\u4ee5\u5728\u63a5\u53e3\u91cc\u5b9a\u4e49\u9759\u6001\u65b9\u6cd5\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u628a\u5de5\u5177\u7c7b\u91cc\u7684\u9759\u6001\u65b9\u6cd5\u76f4\u63a5\u79fb\u5230\u63a5\u53e3\u7c7b\u91cc\uff0c\u4e0d\u9700\u8981\u72ec\u7acb\u521b\u5efa\u5de5\u5177\u7c7b\u4e86\u3002<\/p>\n<p>\u4e0b\u9762\u662fJava8\u91cc\u6dfb\u52a0\u5230\u63a5\u53e3 <code>java.util.Comparator<\/code> \u7684\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">public static &lt;T extends Comparable&lt;? super T&gt;&gt; Comparator&lt;T&gt; naturalOrder() {\r\n    return (Comparator&lt;T&gt;)   Comparators.NaturalOrderComparator.INSTANCE;\r\n}\r\n<\/code><\/pre>\n<h3>\u5b9a\u4e49\u9ed8\u8ba4\u65b9\u6cd5<\/h3>\n<p>\u53e6\u4e00\u4e2a\u6539\u8fdb\u662f\uff0c\u53ef\u4ee5\u5728\u4e0d\u7834\u574f\u73b0\u6709\u63a5\u53e3\u5b9e\u73b0\u7684\u524d\u63d0\u4e0b\u6dfb\u52a0\u9ed8\u8ba4\uff08default\uff09\u65b9\u6cd5\u3002\u6bd4\u5982 <code>java.lang.Iterable<\/code> \u63a5\u53e3\u73b0\u5728\u62e5\u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684 <code>forEach<\/code> \u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">public default void forEach(Consumer&lt;? super T&gt; action) {\r\n     Objects.requireNonNull(action);\r\n     for (T t : this) {\r\n          action.accept(t);\r\n     }\r\n}\r\n<\/code><\/pre>\n<h3>\u9ed8\u8ba4\u65b9\u6cd5\u4e0d\u80fd\u8986\u76d6 <code>equals, hashCode, toString<\/code> \u65b9\u6cd5<\/h3>\n<p>\u4e00\u4e2a\u63a5\u53e3\u4e0d\u80fd\u63d0\u4f9b<code>Object<\/code>\u7c7b\u91cc\u7684\u4efb\u4f55\u65b9\u6cd5\u7684\u9ed8\u8ba4\u5b9e\u73b0\u3002\u8fd9\u610f\u5473\u7740\u4e0d\u80fd\u5728\u63a5\u53e3\u91cc\u63d0\u4f9b <code>equals, hashCode, toString<\/code> \u65b9\u6cd5\u7684\u9ed8\u8ba4\u5b9e\u73b0\u3002<\/p>\n<p>\u5173\u4e8e\u8fd9\u4e2a\uff0cBrian Goetz \u7ed9\u51fa\u4e86\u56db\u4e2a\u7406\u7531\uff0c\u5177\u4f53\u89c1\uff1a<a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/lambda-dev\/2013-March\/008435.html\"> response to &#8220;Allow default methods to override Object&#8217;s methods&#8221;<\/a>\u3002<\/p>\n<p>\u8fd9\u91cc\u5217\u51fa\u4e00\u4e2a\u5177\u6709\u8db3\u591f\u8bf4\u670d\u529b\u7684\u7406\u7531\uff1a<\/p>\n<blockquote><p>\n  It would become more difficult to reason about when a default method is invoked. Right now it&#8217;s simple: if a class implements a method, that always wins over a default implementation. Since all instances of interfaces are Objects, all instances of interfaces have non-default implementations of equals\/hashCode\/toString already. Therefore, a default version of these on an interface is always useless, and it may as well not compile.\n<\/p><\/blockquote>\n<p>\u5927\u610f\u662f\uff1a\u7531\u4e8e\u6240\u6709\u63a5\u53e3\u7684\u5b9e\u4f8b\u90fd\u662fObjects\uff0c\u90a3\u4e48\u6240\u6709\u5b9e\u4f8b\u90fd\u5df2\u6709 <code>equals\/hashCode\/toString<\/code> \u8fd9\u4e9b\u65b9\u6cd5\u7684\u975e\u9ed8\u8ba4\u5b9e\u73b0\u3002\u56e0\u6b64\uff0c\u63a5\u53e3\u4e0a\u8fd9\u4e9b\u65b9\u6cd5\u7684\u9ed8\u8ba4\u7248\u672c\u662f\u6ca1\u6709\u7528\u7684\uff0c\u53ef\u80fd\u4e0d\u4f1a\u88ab\u7f16\u8bd1\u3002<\/p>\n<h2>\u51fd\u6570\u63a5\u53e3<\/h2>\n<p>\u51fd\u6570\u63a5\u53e3\u662fJava8\u5f15\u5165\u7684\u4e00\u4e2a\u6838\u5fc3\u6982\u5ff5\u3002\u5982\u679c\u4e00\u4e2a\u63a5\u53e3\u53ea\u5b9a\u4e49\u4e86\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\uff0c\u5b83\u5c31\u662f\u51fd\u6570\u63a5\u53e3\u3002\u4f8b\u5982<code>java.lang.Runnable<\/code>\u5c31\u662f\u4e00\u4e2a\u51fd\u6570\u63a5\u53e3\uff0c\u56e0\u4e3a\u5b83\u53ea\u5b9a\u4e49\u4e86\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\uff1a <code>public  void  run()<\/code>\u3002<\/p>\n<p>\u9ed8\u8ba4\u65b9\u6cd5\u4e0d\u662f\u62bd\u8c61\u7684\uff0c\u6240\u4ee5\u4e00\u4e2a\u51fd\u6570\u63a5\u53e3\u53ef\u4ee5\u5b9a\u4e49\u4efb\u610f\u591a\u7684\u9ed8\u8ba4\u65b9\u6cd5\u3002<\/p>\n<p>\u65b0\u5f15\u5165\u7684\u6ce8\u89e3 <code>@FunctionalInterface<\/code> \u7528\u6765\u8868\u660e\u63a5\u53e3\u51c6\u5907\u6210\u4e3a\u51fd\u6570\u63a5\u53e3\uff0c\u4f46\u4e0d\u7ba1\u6709\u6ca1\u6709\u8fd9\u4e2a\u6ce8\u89e3\uff0c\u7f16\u8bd1\u5668\u90fd\u8ba4\u4e3a\u53ea\u6709\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u7684\u63a5\u53e3\u662f\u51fd\u6570\u63a5\u53e3\u3002<\/p>\n<h2>Lambdas<\/h2>\n<p>\u51fd\u6570\u63a5\u53e3\u4e00\u4e2a\u6781\u5177\u4ef7\u503c\u7684\u5c5e\u6027\u662f\u53ef\u4ee5\u5728lambdas\u91cc\u5b9e\u4f8b\u5316\u3002\u4e0b\u9762\u662flambdas\u7684\u4e00\u4e9b\u5b9e\u4f8b\uff1a<\/p>\n<pre><code class=\"java\">(param1 ParamType1,  param2 ParamTeyp2, ... )  -&gt;  {  statements;  }          \/\/  lambdas \u5b8c\u6574\u5f62\u5f0f\r\n\r\n(int x, int y) -&gt; { return x + y; }          \/\/  \u5b8c\u6574\u8f93\u5165\u53c2\u6570\u7c7b\u578b\uff0c\u5177\u6709\u8fd4\u56de\u8bed\u53e5\u7684\u8bed\u53e5\u5757\r\n\r\n(x, y) -&gt; x + y       \/\/  \u81ea\u52a8\u7c7b\u578b\u63a8\u5bfc\u53c2\u6570\u7c7b\u578b\uff0c\u8bed\u53e5\u5757\u53ea\u6709\u4e00\u6761 return \u8bed\u53e5\u65f6\u53ef\u4ee5\u7701\u7565\u5927\u62ec\u53f7\u548creturn\u5173\u952e\u5b57\uff0c\r\n\r\nx  -&gt;  x * x          \/\/  \u53ea\u6709\u4e00\u4e2a\u8f93\u5165\u53c2\u6570\uff0c\u53ef\u4ee5\u7701\u7565\u5165\u53c2\u5217\u8868\u7684\u5c0f\u62ec\u53f7\u3002\r\n\r\n()  -&gt;  x            \/\/  \u5982\u679c\u6ca1\u6709\u5165\u53c2\uff0c\u5c0f\u62ec\u53f7\u4e0d\u80fd\u7701\u7565\u3002\r\n\r\nx  -&gt;  { System.out.println(x); }     \/\/  \u6ca1\u6709\u8fd4\u56de\u8bed\u53e5\uff0c\u8bed\u53e5\u5757\u7684\u5927\u62ec\u53f7\u4e0d\u80fd\u7701\u7565\u3002\r\n<\/code><\/pre>\n<h3>\u65b9\u6cd5\u5f15\u7528<\/h3>\n<p>\u9759\u6001\u65b9\u6cd5\u5f15\u7528\uff1a <code>String::valueOf<\/code>\u3002<\/p>\n<p>\u975e\u9759\u6001\u65b9\u6cd5\u5f15\u7528\uff1a <code>Object::toString<\/code>\u3002<\/p>\n<p>\u6784\u9020\u51fd\u6570\u5f15\u7528\uff1a <code>ArrayList::new<\/code>\u3002<\/p>\n<p>\u6355\u83b7\u65b9\u6cd5\u5f15\u7528\uff1a <code>x::toString<\/code>\u3002<\/p>\n<p>\u65b9\u6cd5\u5f15\u7528\u7b49\u4ef7\u7684lambda\u8868\u8fbe\u5f0f\uff1a<\/p>\n<pre><code class=\"java\">String::valueOf          x -&gt; String.valueOf(x)\r\nObject::toString         x -&gt; x.toString()\r\nx::toString                 () -&gt; x.toString()                    \/\/  \u8fd9\u4e2a\u597d\u50cf\u4e0d\u884c\r\nArrayList::new           () -&gt; new ArrayList&lt;&gt;()\r\n<\/code><\/pre>\n<p>\u5f53\u7136\uff0c\u5728Java\u91cc\u53ef\u4ee5\u91cd\u8f7d\u65b9\u6cd5\u3002\u7c7b\u53ef\u4ee5\u6709\u5f88\u591a\u6709\u540c\u6837\u540d\u5b57\u4f46\u4e0d\u540c\u53c2\u6570\u7684\u65b9\u6cd5\uff0c\u6784\u9020\u51fd\u6570\u4e5f\u4e00\u6837\u3002\u7528\u54ea\u4e2a\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5b83\u662f\u7528\u4e8e\u54ea\u4e2a\u51fd\u6570\u63a5\u53e3\u7684\u3002<\/p>\n<p>\u4e00\u4e2alambda\u4e0e\u4e00\u4e2a\u51fd\u6570\u63a5\u53e3\u6709\u76f8\u540c\u7684\u201c\u5f62\u72b6\u201d\u65f6\u88ab\u8ba4\u4e3a\u662f\u517c\u5bb9\u7684\u3002\u8fd9\u91cc\u7684\u201c\u5f62\u72b6\u201d\u662f\u6307\uff1a\u8f93\u5165\u8f93\u51fa\u7684\u7c7b\u578b\u548c\u58f0\u660e\u7684\u53d7\u68c0\u67e5\u5f02\u5e38\u3002<\/p>\n<h4>\u4e3e\u4f8b<\/h4>\n<pre><code class=\"java\">List&lt;String&gt; strList = new ArrayList&lt;&gt;();\r\nstrList.add(\"no\");\r\nstrList.add(\"hello\");\r\nstrList.add(\"world\");\r\n\r\nlong count = strList\r\n          .stream()\r\n          .filter((String str) -&gt; { return str != null; })     \/\/  \u5b8c\u6574\u7684lambda\u8868\u8fbe\u5f0f\r\n          .filter(str -&gt; { return str.length() &gt; 0; })     \/\/  \u53c2\u6570\u7c7b\u578b\u81ea\u52a8\u63a8\u5bfc\uff1b\u53ea\u6709\u4e00\u4e2a\u8f93\u5165\u53c2\u6570\uff0c\u7701\u7565\u5c0f\u62ec\u53f7\r\n          .filter(str -&gt; str.length() &gt; 3)     \/\/  \u8bed\u53e5\u5757\u53ea\u662f\u8fd4\u56de\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u7701\u7565return\u8bed\u53e5\u76f4\u63a5\u8fd4\u56de\u8868\u8fbe\u5f0f\u7684\u503c\uff1b\u7701\u7565\u8bed\u53e5\u5757\u7684\u82b1\u62ec\u53f7\u3002\r\n          .map(String::length)                 \/\/  \u7528\u65b9\u6cd5\u4ee3\u66fflambda\u8868\u8fbe\u5f0f\r\n          .count();\r\nSystem.out.println(\"\\n\\ncount:\" + count);\r\n\r\n\r\nRunnable r = () -&gt; { System.out.println(\"Running!\"); };\r\nComparator&lt;String&gt; c = (a, b) -&gt; Integer.compare(a.length(), b.length());\r\n<\/code><\/pre>\n<h3>\u6355\u83b7\u4e0e\u975e\u6355\u83b7lambdas<\/h3>\n<p>\u5982\u679clambda\u8868\u8fbe\u5f0f\u8bbf\u95ee\u4e00\u4e2a\u5b9a\u4e49\u5728lambda\u4f53\u4e4b\u5916\u7684\u975e\u9759\u6001\u53d8\u91cf\u6216\u5bf9\u8c61\uff0c\u5219\u8bf4Lambda\u8868\u8fbe\u5f0f\u662f\u201c\u6355\u83b7\u7684\u201d\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u91cc\uff0creturn\u8fd4\u56de\u7684lambda\u6355\u83b7\u4e86\u53d8\u91cf <code>x<\/code>\uff1a<\/p>\n<pre><code class=\"java\">int x = 5;\r\nreturn y -&gt; x + y;\r\n<\/code><\/pre>\n<p>\u4e3a\u4e86\u8ba9\u8fd9\u4e2alambda\u58f0\u660e\u662f\u5408\u6cd5\u7684\uff0c\u5b83\u6355\u83b7\u7684\u53d8\u91cf\u5fc5\u987b\u662f\u201ceffectively  final\u201d\u3002\u8fd9\u6837\uff0c\u5b83\u4eec\u5fc5\u987b\u6807\u8bb0\u4e3a <code>final<\/code> \u6216 \u8d4b\u503c\uff08\u88ablambda\u6355\u83b7\uff09\u4e4b\u540e\u5c31\u4e0d\u80fd\u518d\u4fee\u6539\u3002<\/p>\n<p>\u4e00\u4e2alambda\u6355\u83b7\u4e0e\u5426\u5bf9\u6027\u80fd\u6709\u5f71\u54cd\u3002\u4e00\u4e2a\u975e\u6355\u83b7\u7684lambda\u4e00\u822c\u6bd4\u4e00\u4e2a\u6355\u83b7\u7684\u66f4\u9ad8\u6548\u3002\u867d\u7136\u8fd9\u6ca1\u6709\u5728\u4efb\u4f55\u89c4\u8303\u91cc\u5b9a\u4e49\uff0c\u4f60\u4e0d\u80fd\u4f9d\u8d56\u4e8e\u7a0b\u5e8f\u5458\u7684\u6b63\u786e\u6027\u3002<strong>\u4e00\u4e2a\u975e\u6355\u83b7\u7684lambda\u53ea\u9700\u8981\u8ba1\u7b97\u4e00\u6b21\u3002<\/strong>\u7136\u540e\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u5b8c\u5168\u76f8\u540c\u7684\u5b9e\u4f8b\u3002\u6355\u83b7\u7684lambda\u5728\u6bcf\u6b21\u9047\u5230\u65f6\u90fd\u9700\u8981\u8fdb\u884c\u8ba1\u7b97\uff0c\u5f53\u524d\u7684\u52a8\u4f5c\u4e0e\u5b9e\u4f8b\u5316\u533f\u540d\u5185\u90e8\u7c7b\u975e\u5e38\u7c7b\u4f3c\u3002<\/p>\n<h4>\u4e3e\u4f8b<\/h4>\n<pre><code class=\"java\">private static int count = 0;\r\npublic static void main(String[] args) {\r\n\/\/          basic ();\r\n     testCapture();\r\n}\r\n\r\npublic static void testCapture() {\r\n     Runnable run = capture();\r\n     count++;\r\n     Runnable run2 = capture();\r\n     new Thread(run).start();\r\n     new Thread(run2).start();\r\n}\r\n\r\npublic static Runnable capture() {\r\n     int x = 5;\r\n     Runnable r = () -&gt; { System.out.println(\"x is:\" + x); System.out.println(\"count is:\" + count); };\r\n     \/\/ \u5728\u8fd9\u91cc\u7ed9x\u8d4b\u503c\u4e5f\u4f1a\u62a5\u9519  Variable used in lambda expression should be effectively final\r\n     return r;\r\n}\r\n<\/code><\/pre>\n<p>\u6267\u884c\u540e\u7684\u8f93\u51fa\u662f\uff1a<\/p>\n<pre><code>x is:5\r\ncount is:1\r\nx is:5\r\ncount is:1\r\n<\/code><\/pre>\n<p>\u5bf9\u4e8ex\u7684\u503c\u90fd\u6ca1\u6709\u95ee\u9898\uff0c\u4f46count\u7684\u503c\u5c31\u8981\u6ce8\u610f\u4e86\u3002\u8fdb\u5165<code>testCapture()<\/code>\u65b9\u6cd5\u7684\u65f6\u5019\uff0ccount\u7684\u503c\u662f0\uff0c\u7b2c\u4e00\u6b21\u6355\u83b7\u7684\u65f6\u5019\u4e5f\u662f0\uff0c\u7136\u540e\u8fdb\u884c\u52a01\uff0c\u518d\u6b21\u8fdb\u884c\u6355\u83b7\uff0c\u7136\u540e\u7528\u4e24\u4e2a\u7ebf\u7a0b\u8ba1\u7b97\u6355\u83b7\u7684\u503c\uff0c\u5f97\u5230\u7684\u503c\u662f1\u548c1\uff0c\u800c\u4e0d\u662f0\u548c1\uff0c\u56e0\u4e3a\uff0c<strong>lambda\u6355\u83b7\u53ea\u662f\u6355\u83b7\u4e86\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f\u53d8\u91cf\u7684\u503c\u3002<\/strong><\/p>\n<h3>lambdas \u4e0d\u80fd\u505a\u7684<\/h3>\n<p>\u6709\u4e9b\u7ea6\u675f\u662flambdas\u4e0d\u63d0\u4f9b\u7684\uff0c\u4e3a\u4e86\u7b80\u5355\u548c\u7531\u4e8e\u65f6\u95f4\u7ea6\u675f\u3002<\/p>\n<h4>\u975efinal\u53d8\u91cf\u6355\u83b7<\/h4>\n<p>\u5982\u679c\u4e00\u4e2a\u53d8\u91cf\u88ab\u8d4b\u4e88\u65b0\u503c\uff0c\u5b83\u5c31\u4e0d\u80fd\u7528\u5728lambda\u91cc\u3002\u201cfinal\u201d\u5173\u952e\u5b57\u4e0d\u662f\u5fc5\u987b\u7684\uff0c\u4f46\u53d8\u91cf\u5fc5\u987b\u662f\u201ceffectively  final\u201d\uff08\u5982\u524d\u6240\u8ff0\uff09\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u5c06\u62a5\u7f16\u8bd1\u9519\u8bef\uff1a<\/p>\n<pre><code class=\"java\">int count = 0;\r\nList&lt;String&gt; strings = Arrays.asList(\"a\", \"b\", \"c\");\r\nstrings.forEach(s -&gt; {\r\n    count++; \/\/ error: can't modify the value of count\r\n});\r\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u5982\u679ccount\u662f\u4e2a\u975e\u5c40\u90e8\u53d8\u91cf\uff0c\u5b83\u5c06\u662f\u53ef\u4ee5\u4fee\u6539\u7684\u3002<\/p>\n<h4>\u5f02\u5e38\u900f\u660e<\/h4>\n<p>\u5982\u679clambda\u4ee3\u7801\u4f53\u4f1a\u629b\u51fa\u53d7\u68c0\u67e5\u7684\u5f02\u5e38\uff0c\u51fd\u6570\u63a5\u53e3\u5fc5\u987b\u58f0\u660e\u5b83\u4f1a\u629b\u51fa\u7684\u53d7\u68c0\u67e5\u5f02\u5e38\u3002\u5f02\u5e38\u4e0d\u4f1a\u4f20\u64ad\u5230\u5305\u542b\u65b9\u6cd5\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u4e0d\u80fd\u7f16\u8bd1\uff1a<\/p>\n<pre><code class=\"java\">void appendAll(Iterable&lt;String&gt; values, Appendable out)\r\n        throws IOException { \/\/ doesn't help with the error\r\n    values.forEach(s -&gt; {\r\n        out.append(s); \/\/ error: can't throw IOException here\r\n        \/\/ Consumer.accept(T) doesn't allow it\r\n    });\r\n}\r\n<\/code><\/pre>\n<p>\u7b80\u5355\u8bf4\u5c31\u662flambda\u7684\u4ee3\u7801\u5757\u4e0d\u80fd\u629b\u51fa\u53d7\u68c0\u67e5\u5f02\u5e38\u3002<\/p>\n<h4>\u6d41\u7a0b\u63a7\u5236\uff08break\uff0cearly return\uff09<\/h4>\n<p>\u5728\u4e0a\u9762\u7684forEach\u4f8b\u5b50\u91cc\uff0c\u4f20\u7edf\u7684\u201ccontinue\u201d\u53ef\u4ee5\u662f\u901a\u8fc7lambda\u4f53\u5185\u7684\u201creturn;\u201d\u8bed\u53e5\u6765\u8fbe\u5230\u3002\u7136\u800c\uff0c\u6ca1\u6709\u65b9\u6cd5\u6765\u4e2d\u65adloop\u5faa\u73af\u6216\u4ecelambda\u5185\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u5230\u5305\u542b\u65b9\u6cd5\u3002\u4e3e\u4f8b\uff1a<\/p>\n<pre><code class=\"java\">import java.util.Arrays;\r\n\r\npublic class Lambdas2 {\r\n     public static void main(String[] args) {\r\n        Arrays.asList(args).forEach(s -&gt; {\r\n            if (s.length() &gt; 3) {\r\n                return;     \/\/  \u8fd9\u4e2areturn\u8bed\u53e5\u5e76\u4e0d\u80fd\u7ec8\u6b62loop\u5faa\u73af\u3002\r\n            }\r\n        });\r\n     }\r\n}\r\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u751f\u6210\u7684\u5b57\u8282\u7801\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code>$ javap -c Lambdas2.class\r\nCompiled from \"Lambdas2.java\"\r\npublic class Lambdas2 {\r\n  public Lambdas2();\r\n    Code:\r\n       0: aload_0\r\n       1: invokespecial #1                  \/\/ Method java\/lang\/Object.\"&lt;init&gt;\":()V\r\n       4: return\r\n\r\n  public static void main(java.lang.String[]);\r\n    Code:\r\n       0: aload_0\r\n       1: invokestatic  #2                  \/\/ Method java\/util\/Arrays.asList:([Ljava\/lang\/Object;)Ljava\/util\/List;\r\n       4: invokedynamic #3,  0              \/\/ InvokeDynamic #0:accept:()Ljava\/util\/function\/Consumer;\r\n       9: invokeinterface #4,  2            \/\/ InterfaceMethod java\/util\/List.forEach:(Ljava\/util\/function\/Consumer;)V\r\n      14: return\r\n}\r\n<\/code><\/pre>\n<p>\u7ed3\u5408\u524d\u9762 <code>java.lang.Iterable<\/code> \u63a5\u53e3\u7684<code>forEach<\/code>\u65b9\u6cd5\u5b9e\u73b0\u53ef\u77e5\uff0c\u76ee\u524d\u7684lambda\u5e94\u8be5\u662f\u7f16\u8bd1\u6210\u4e00\u4e2a\u5bf9\u8c61\u7684\uff0c\u7136\u540e\u518d\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8c03\u7528\u6b64\u5bf9\u8c61\u7684<code>accept<\/code>\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u8bf4lambda\u4ee3\u7801\u5757\u88ab\u7f16\u8bd1\u6210Consumer\u7684accept\u65b9\u6cd5\u7684\u4ee3\u7801\uff0c\u6240\u4ee5\u4e0d\u80fd\u5728lambda\u4ee3\u7801\u5757\u91cc\u63a7\u5236<code>forEach<\/code>\u7684loop\u6d41\u7a0b\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u62bd\u8c61\u7c7b\u4e0d\u80fd\u7528lambda\u5b9e\u4f8b\u5316\uff1f<\/h3>\n<p>\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u5373\u4f7f\u5b83\u53ea\u6709\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\uff0c\u4e5f\u4e0d\u80fd\u7528lambda\u5b9e\u4f8b\u5316\u3002<\/p>\n<p>\u5927\u591a\u6570\u53cd\u5bf9\u8fd9\u4e2a\u7684\u4e89\u8bba\u662f\u8fd9\u4f1a\u589e\u52a0\u9605\u8bfblambda\u7684\u56f0\u96be\u3002\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u4f8b\u5316\u4e00\u4e2a\u62bd\u8c61\u7c7b\u4f1a\u5bfc\u81f4\u6267\u884c\u989d\u5916\u7684\u9690\u85cf\u4ee3\u7801\uff1a\u62bd\u8c61\u7c7b\u6784\u9020\u5668\u91cc\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u7406\u7531\u662f\u8fd9\u629b\u5f03\u4e86\u4f18\u5316lambda\u7684\u53ef\u80fd\u3002\u5728\u5c06\u6765\uff0clambda\u53ef\u80fd\u5c06\u4e0d\u518d\u5b9e\u4f8b\u5316\u4e3a\u5bf9\u8c61\u3002\u5141\u8bb8\u7528\u6237\u4ee5lambda\u7684\u5f62\u5f0f\u58f0\u660e\u62bd\u8c61\u7c7b\u5c06\u963b\u6b62\u8fd9\u7c7b\u4f18\u5316\u3002<\/p>\n<p>\u66f4\u591a\u89e3\u91ca\u89c1\uff1a<a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/lambda-dev\/2013-March\/008441.html\">response to &#8220;Allow lambdas to implement abstract classes&#8221;<\/a><\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\u81ea\uff1ahttp:\/\/www.techempower.com\/blog\/2013 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20130914\/467\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[91,92],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/467"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=467"}],"version-history":[{"count":9,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/467\/revisions"}],"predecessor-version":[{"id":1756,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/467\/revisions\/1756"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}