{"id":203077,"date":"2025-05-22T12:37:48","date_gmt":"2025-05-22T04:37:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203077\/"},"modified":"2025-05-22T12:37:48","modified_gmt":"2025-05-22T04:37:48","slug":"%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e4%b8%ad%e7%9a%84-lambda-%e8%a1%a8%e8%be%be%e5%bc%8f%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203077\/","title":{"rendered":"\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684 lambda \u8868\u8fbe\u5f0f\u5982\u4f55\u5904\u7406\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684 lambda \u8868\u8fbe\u5f0f\u5982\u4f55\u5904\u7406\u5f02\u5e38\uff1f<\/h1>\n<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684 lambda \u8868\u8fbe\u5f0f\u5982\u4f55\u5904\u7406\u5f02\u5e38\uff1f\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0cLambda \u8868\u8fbe\u5f0f\u5f02\u5e38\u5904\u7406\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u629b\u51fa\u5f02\u5e38\u3001\u4f7f\u7528 try-catch \u5757\u3001\u4f7f\u7528 Error Handling \u51fd\u6570\u3002\u5176\u4e2d\uff0c\u629b\u51fa\u5f02\u5e38\u5c06\u6267\u884c\u6d41\u4f20\u9012\u56de\u8c03\u7528 Lambda \u8868\u8fbe\u5f0f\u7684\u4ee3\u7801\uff0ctry-catch \u5757\u5141\u8bb8\u5728 Lambda \u8868\u8fbe\u5f0f\u5185\u90e8\u5904\u7406\u5f02\u5e38\uff0cError Handling \u51fd\u6570\u5141\u8bb8 Lambda \u8868\u8fbe\u5f0f\u4ee5\u58f0\u660e\u65b9\u5f0f\u5904\u7406\u5f02\u5e38\u3002\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u4ee3\u7801\u4f7f\u7528 try-catch \u5757\u5904\u7406\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5f02\u5e38\uff0c\u5e76\u5728\u5339\u914d\u5931\u8d25\u65f6\u5c06\u884c\u6253\u5370\u5230\u6807\u51c6\u9519\u8bef\u6d41\u4e2d\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730040526671e52cec6aaa.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684 Lambda \u8868\u8fbe\u5f0f\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0cLambda \u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u8868\u8fbe\u533f\u540d\u51fd\u6570\u3002\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u5904\u7406\u96c6\u5408\u6570\u636e\uff0c\u4f8b\u5982\u5217\u8868\u6216\u6570\u7ec4\u3002<\/p>\n<p><strong>\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>\u5f53 Lambda \u8868\u8fbe\u5f0f\u6267\u884c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5f02\u5e38\u3002\u4e0e\u5e38\u89c4\u51fd\u6570\u7c7b\u4f3c\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff1a<\/p>\n<p><strong>1. \u629b\u51fa\u5f02\u5e38<\/strong><\/p>\n<p>Lambda \u8868\u8fbe\u5f0f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 <code>throw<\/code> \u5173\u952e\u5b57\u629b\u51fa\u5f02\u5e38\u3002\u8fd9\u4f1a\u5c06\u6267\u884c\u6d41\u4f20\u9012\u56de\u8c03\u7528 Lambda \u8868\u8fbe\u5f0f\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>\u4f8b\uff1a<\/strong><\/p>\n<pre>List&lt;Integer&gt; numbers = List.of(1, 2, 3, 4, 5);\n\nnumbers.forEach(number -&gt; {\n    if (number &lt; 0) {\n        throw new IllegalArgumentException(\"\u8d1f\u6570\u4e0d\u5141\u8bb8\");\n    }\n});<\/pre>\n<p><strong>2. \u4f7f\u7528 Try-Catch<\/strong><\/p>\n<p>Lambda \u8868\u8fbe\u5f0f\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>try-catch<\/code> \u5757\u6765\u6355\u83b7\u5f02\u5e38\u3002\u8fd9\u5141\u8bb8\u5728 Lambda \u8868\u8fbe\u5f0f\u5185\u90e8\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p><strong>\u4f8b\uff1a<\/strong><\/p>\n<pre>numbers.forEach(number -&gt; {\n    try {\n        if (number &lt; 0) {\n            throw new IllegalArgumentException(\"\u8d1f\u6570\u4e0d\u5141\u8bb8\");\n        }\n    } catch (IllegalArgumentException e) {\n        \/\/ \u5728\u6b64\u5904\u7406\u5f02\u5e38\n    }\n});<\/pre>\n<p><strong>3. \u4f7f\u7528 Error Handling \u51fd\u6570<\/strong><\/p>\n<p>\u67d0\u4e9b\u51fd\u6570\u5f0f\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b Error Handling \u51fd\u6570\u3002\u8fd9\u4e9b\u51fd\u6570\u5141\u8bb8 Lambda \u8868\u8fbe\u5f0f\u4ee5\u58f0\u660e\u65b9\u5f0f\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p><strong>\u4f8b\uff1a<\/strong><\/p>\n<pre>numbers.foreach(number =&gt; {\n    if (number &lt; 0) Either.left(new IllegalArgumentException(\"\u8d1f\u6570\u4e0d\u5141\u8bb8\"))\n    else Either.right(number)\n})<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\u5904\u7406\u6587\u672c\u6587\u4ef6\u7684\u6848\u4f8b\u3002\u6211\u4eec\u5e0c\u671b\u5c06\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u884c\u5339\u914d\u6307\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u5339\u914d\u5931\u8d25\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p><strong>Java \u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>import java.io.BufferedReader;\nimport java.io.File;\nimport java.io.FileReader;\nimport java.util.Arrays;\nimport java.util.List;\nimport java.util.regex.Pattern;\nimport java.util.stream.Collectors;\n\npublic class RegexMatcher {\n\n    public static void main(String[] args) {\n        File file = new File(\"text.txt\");\n        Pattern pattern = Pattern.compile(\"^[A-Za-z0-9]*$\");\n\n        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {\n            List&lt;String&gt; lines = reader.lines().collect(Collectors.toList());\n\n            lines.stream()\n                    .map(line -&gt; processLine(line, pattern))\n                    .filter(result -&gt; result.isLeft())\n                    .forEach(result -&gt; System.err.println(result.left().get()));\n        } catch (Exception e) {\n            System.err.println(\"\u65e0\u6cd5\u8bfb\u53d6\u6587\u4ef6\uff1a\" + e.getMessage());\n        }\n    }\n\n    private static Either&lt;Exception, String&gt; processLine(String line, Pattern pattern) {\n        try {\n            if (!pattern.matcher(line).matches()) {\n                throw new IllegalArgumentException(\"\u884c\u4e0d\u5339\u914d\u6a21\u5f0f\");\n            }\n            return Either.right(line);\n        } catch (Exception e) {\n            return Either.left(e);\n        }\n    }\n}<\/pre>\n<p>\u6b64\u4ee3\u7801\u4f7f\u7528 <code>try-catch<\/code> \u5757\u5728 Lambda \u8868\u8fbe\u5f0f\u4e2d\u5904\u7406\u5f02\u5e38\u3002\u5b83\u8fd8\u4f1a\u8fc7\u6ee4\u6389\u5904\u7406\u6210\u529f\u7684\u884c\uff0c\u5e76\u5c06\u5339\u914d\u5931\u8d25\u7684\u884c\u6253\u5370\u5230\u6807\u51c6\u9519\u8bef\u6d41\u4e2d\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684 lambda \u8868\u8fbe\u5f0f\u5982\u4f55\u5904\u7406\u5f02\u5e38\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684 lambda \u8868&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203077","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203077","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203077"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203077\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}