

{"id":945,"date":"2021-02-18T21:15:46","date_gmt":"2021-02-18T13:15:46","guid":{"rendered":"https:\/\/www.52dixiaowo.com\/java\/?p=945"},"modified":"2021-02-18T21:15:47","modified_gmt":"2021-02-18T13:15:47","slug":"function-%e5%87%bd%e6%95%b0%e5%bc%8f%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/www.52dixiaowo.com\/java\/post-945.html","title":{"rendered":"Function \u51fd\u6570\u5f0f\u63a5\u53e3"},"content":{"rendered":"\n<p>\u6b64\u63a5\u53e3\u662f\u4e00\u4e2a\u8f6c\u6362\u578b\u63a5\u53e3<\/p>\n\n\n\n<h5>1.\u5bfc\u5305<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.function.Function;<\/code><\/pre>\n\n\n\n<h5>2.\u65b9\u6cd5<\/h5>\n\n\n\n<p>1.\u6b64\u63a5\u53e3\u5305\u542b\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5 R apply(T t)<\/p>\n\n\n\n<p>\u6b64\u63a5\u53e3\u662f\u53cc\u5217\u6cdb\u578b\uff0c\u5373 Function&lt;T,R> \u6839\u636e T \u7c7b\u578b\u5f97\u5230 R \u7c7b\u578b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.function.Function;\npublic class Demo {\r\n    \/*\r\n        \u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\r\n        \u65b9\u6cd5\u7684\u53c2\u6570\u4f20\u9012\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6574\u6570\r\n        \u65b9\u6cd5\u7684\u53c2\u6570\u4f20\u9012\u4e00\u4e2aFunction\u63a5\u53e3,\u6cdb\u578b\u4f7f\u7528&lt;String,Integer>\r\n        \u4f7f\u7528Function\u63a5\u53e3\u4e2d\u7684\u65b9\u6cd5apply,\u628a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6574\u6570,\u8f6c\u6362\u4e3aInteger\u7c7b\u578b\u7684\u6574\u6570\r\n     *\/\r\n    public static void change(String s, Function&lt;String,Integer> fun){\r\n        int in = fun.apply(s);\/\/\u81ea\u52a8\u62c6\u7bb1 Integer->int\r\n        System.out.println(in);\r\n    }\r\n\r\n    public static void main(String&#91;] args) {\r\n        \/\/\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6574\u6570\r\n        String s = \"1234\";\r\n        \/\/\u8c03\u7528change\u65b9\u6cd5,\u4f20\u9012\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6574\u6570,\u548cLambda\u8868\u8fbe\u5f0f,\u628a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6574\u6570,\u8f6c\u6362\u4e3aInteger\u7c7b\u578b\u7684\u6574\u6570\u8fd4\u56de\r\n        change(s,str->Integer.parseInt(str));\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>2.\u63a5\u53e3\u7684\u9ed8\u8ba4\u65b9\u6cd5 andThen<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.function.Function;\npublic class Demo {\r\n    \/*\r\n        \u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\r\n        \u53c2\u6570\u4e32\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6574\u6570\r\n        \u53c2\u6570\u518d\u4f20\u9012\u4e24\u4e2aFunction\u63a5\u53e3\r\n            \u4e00\u4e2a\u6cdb\u578b\u4f7f\u7528Function&lt;String,Integer>\r\n            \u4e00\u4e2a\u6cdb\u578b\u4f7f\u7528Function&lt;Integer,String>\r\n     *\/\r\n    public static void change(String s, Function&lt;String,Integer> fun1,Function&lt;Integer,String> fun2){\r\n        String ss = fun1.andThen(fun2).apply(s);\r\n        System.out.println(ss);\r\n    }\r\n\r\n    public static void main(String&#91;] args) {\r\n        \/\/\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6574\u6570\r\n        String s = \"123\";\r\n        \/\/\u8c03\u7528change\u65b9\u6cd5,\u4f20\u9012\u5b57\u7b26\u4e32\u548c\u4e24\u4e2aLambda\u8868\u8fbe\u5f0f,\u628a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570+10,\u628a\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\r\n        change(s,str->Integer.parseInt(str)+10,i->i+\"\");\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6b64\u63a5\u53e3\u662f\u4e00\u4e2a\u8f6c\u6362\u578b\u63a5\u53e3 1.\u5bfc\u5305 2.\u65b9\u6cd5 1.\u6b64\u63a5\u53e3\u5305\u542b\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5 R apply(T t) \u6b64\u63a5\u53e3\u662f\u53cc\u5217&hellip; <a href=\"https:\/\/www.52dixiaowo.com\/java\/post-945.html\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">Function \u51fd\u6570\u5f0f\u63a5\u53e3<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[28],"tags":[],"_links":{"self":[{"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/posts\/945"}],"collection":[{"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/comments?post=945"}],"version-history":[{"count":0,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/posts\/945\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/media?parent=945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/categories?post=945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/tags?post=945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}