{"id":1433,"date":"2022-11-03T23:03:02","date_gmt":"2022-11-03T15:03:02","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1433"},"modified":"2022-11-27T12:39:44","modified_gmt":"2022-11-27T04:39:44","slug":"%e6%96%b9%e6%b3%95%e5%bc%95%e7%94%a8%e7%9a%84%e4%bd%bf%e7%94%a8%e6%83%85%e5%86%b51%e3%80%81%e6%96%b9%e6%b3%95%e5%bc%95%e7%94%a8%e7%9a%84%e4%bd%bf%e7%94%a8%e6%83%85%e5%86%b52%e3%80%81%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1433","title":{"rendered":"\u65b9\u6cd5\u5f15\u7528\u7684\u4f7f\u7528\u60c5\u51b51\u3001\u65b9\u6cd5\u5f15\u7528\u7684\u4f7f\u7528\u60c5\u51b52\u3001\u65b9\u6cd5\u5f15\u7528\u7684\u4f7f\u7528\u60c5\u51b53\u3001\u6784\u9020\u5668\u5f15\u7528\u4e0e\u9879\u76ee\u5f15\u7528\u7684\u4f7f\u7528"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P673;\r\n\r\nimport org.junit.jupiter.api.Test;\r\n\r\nimport java.io.PrintStream;\r\nimport java.util.Comparator;\r\nimport java.util.function.BiPredicate;\r\nimport java.util.function.Consumer;\r\nimport java.util.function.Function;\r\nimport java.util.function.Supplier;\r\n\r\n\/**\r\n * \u65b9\u6cd5\u5f15\u7528\u7684\u4f7f\u7528\r\n *\r\n * 1. \u4f7f\u7528\u60c5\u5883\uff1a\u5f53\u8981\u4f20\u9012\u7ed9Lambda\u4f53\u7684\u64cd\u4f5c\uff0c\u5df2\u7ecf\u6709\u5b9e\u73b0\u7684\u65b9\u6cd5\u4e86\uff0c\u53ef\u4ee5\u4f7f\u7528\u65b9\u6cd5\u5f15\u7528\uff01\r\n *\r\n * 2. \u65b9\u6cd5\u5f15\u7528\uff0c\u672c\u8d28\u4e0a\u5c31\u662fLambda\u8868\u8fbe\u5f0f\uff0c\u800cLambda\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u51fd\u6570\u5f0f\u63a5\u53e3\u7684\u5b9e\u4f8b\u3002\u6240\u4ee5\r\n * \t\t\u65b9\u6cd5\u5f15\u7528\uff0c\u4e5f\u662f\u51fd\u6570\u5f0f\u63a5\u53e3\u7684\u5b9e\u4f8b\u3002\r\n *\r\n * 3. \u4f7f\u7528\u683c\u5f0f\uff1a\t\u7c7b(\u6216\u5bf9\u8c61) :: \u65b9\u6cd5\u540d\r\n *\r\n * 4. \u5177\u4f53\u5206\u4e3a\u5982\u4e0b\u7684\u4e09\u79cd\u60c5\u51b5:\r\n * \t\t\u60c5\u51b51\t\u5bf9\u8c61 :: \u975e\u9759\u6001\r\n * \t\t\u60c5\u51b52\t\u7c7b :: \u9759\u6001\u65b9\u6cd5\r\n *\r\n * \t\t\u60c5\u51b53\t\u7c7b :: \u975e\u9759\u6001\u65b9\u6cd5\r\n *\r\n * 5. \u65b9\u6cd5\u5f15\u7528\u4f7f\u7528\u7684\u8981\u6c42\uff1a\u8981\u6c42\u63a5\u53e3\u4e2d\u7684\u62bd\u8c61\u65b9\u6cd5\u7684\u5f62\u53c2\u5217\u8868\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u4e0e\u65b9\u6cd5\u5f15\u7528\u7684\u65b9\u6cd5\u7684\r\n * \t\t\u5f62\u53c2\u5217\u8868\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u76f8\u540c\uff01\t\uff08\u9488\u5bf9\u4e8e\u60c5\u51b51\u548c\u60c5\u51b52\uff09\r\n *\r\n * Created by shkstart.\r\n *\/\r\npublic class MethodRefTest {\r\n\r\n\t\/\/ \u60c5\u51b5\u4e00\uff1a\u5bf9\u8c61 :: \u5b9e\u4f8b\u65b9\u6cd5\r\n\t\/\/Consumer\u4e2d\u7684void accept(T t)\r\n\t\/\/PrintStream\u4e2d\u7684void println(T t)\r\n\t@Test\r\n\tpublic void test1() {\r\n\t\tConsumer&lt;String> con1 = str -> System.out.println(str);\r\n\t\tcon1.accept(\"\u5317\u4eac\");\r\n\r\n\t\tSystem.out.println(\"***************************\");\r\n\r\n\t\tPrintStream ps = System.out;\r\n\t\tConsumer&lt;String> con2 = ps :: println;\r\n\t\tcon2.accept(\"beijing\");\r\n\t\t\r\n\t}\r\n\t\r\n\t\/\/Supplier\u4e2d\u7684T get()\r\n\t\/\/Employee\u4e2d\u7684String getName()\r\n\t@Test\r\n\tpublic void test2() {\r\n\t\tEmployee emp = new Employee(1001,\"Tom\",23,5600);\r\n\t\tSupplier&lt;String> sup1 = () -> emp.getName();\r\n\t\tSystem.out.println(sup1.get());\r\n\r\n\t\tSystem.out.println(\"***************************\");\r\n\r\n\t\tSupplier&lt;String> sup2 = emp :: getName;\r\n\t\tSystem.out.println(sup2.get());\r\n\r\n\t}\r\n\r\n\t\/\/ \u60c5\u51b5\u4e8c\uff1a\u7c7b :: \u9759\u6001\u65b9\u6cd5\r\n\t\/\/Comparator\u4e2d\u7684int compare(T t1,T t2)\r\n\t\/\/Integer\u4e2d\u7684int compare(T t1,T t2)\r\n\t@Test\r\n\tpublic void test3() {\r\n\t\tComparator&lt;Integer> com1 = (t1,t2) -> Integer.compare(t1,t2);\r\n\t\tSystem.out.println(com1.compare(12, 21));\r\n\r\n\t\tSystem.out.println(\"***************************\");\r\n\r\n\t\tComparator&lt;Integer> com2 = Integer :: compare;\r\n\t\tSystem.out.println(com2.compare(12, 3));\r\n\t}\r\n\t\r\n\t\/\/Function\u4e2d\u7684R apply(T t)\r\n\t\/\/Math\u4e2d\u7684Long round(Double d)\r\n\t@Test\r\n\tpublic void test4() {\r\n\t\tFunction&lt;Double,Long> func = new Function&lt;Double, Long>() {\r\n\t\t\t@Override\r\n\t\t\tpublic Long apply(Double d) {\r\n\t\t\t\treturn Math.round(d);\r\n\t\t\t}\r\n\t\t};\r\n\r\n\t\tSystem.out.println(\"***************************\");\r\n\r\n\t\tFunction&lt;Double,Long> func1 = d -> Math.round(d);\r\n\t\tSystem.out.println(func1.apply(12.3));\r\n\r\n\t\tSystem.out.println(\"***************************\");\r\n\r\n\t\tFunction&lt;Double,Long> func2 = Math :: round;\r\n\t\tSystem.out.println(func2.apply(12.6));\r\n\r\n\t}\r\n\r\n\t\/\/ \u60c5\u51b5\u4e09\uff1a\u7c7b :: \u5b9e\u4f8b\u65b9\u6cd5 (\u6709\u96be\u5ea6)\r\n\t\/\/ Comparator\u4e2d\u7684int comapre(T t1,T t2)\r\n\t\/\/ String\u4e2d\u7684int t1.compareTo(t2)\r\n\t@Test\r\n\tpublic void test5() {\r\n\t\tComparator&lt;String> com1 = (s1,s2) -> s1.compareTo(s2);\r\n\t\tSystem.out.println(com1.compare(\"abc\", \"abd\"));\r\n\r\n\t\tSystem.out.println(\"***************************\");\r\n\r\n\t\tComparator&lt;String> com2 = String :: compareTo;\r\n\t\tSystem.out.println(com1.compare(\"abd\", \"abm\"));\r\n\r\n\t}\r\n\r\n\t\/\/BiPredicate\u4e2d\u7684boolean test(T t1, T t2);\r\n\t\/\/String\u4e2d\u7684boolean t1.equals(t2)\r\n\t@Test\r\n\tpublic void test6() {\r\n\t\tBiPredicate&lt;String,String> pre1 = (s1,s2) -> s1.equals(s2);\r\n\t\tSystem.out.println(pre1.test(\"abc\",\"abc\"));\r\n\r\n\t\tBiPredicate&lt;String,String> pre2 = String :: equals;\r\n\t\tSystem.out.println(pre1.test(\"abc\",\"abd\"));\r\n\t\t\r\n\t\t\r\n\t}\r\n\t\r\n\t\/\/ Function\u4e2d\u7684R apply(T t)\r\n\t\/\/ Employee\u4e2d\u7684String getName();\r\n\t@Test\r\n\tpublic void test7() {\r\n\r\n\t\tEmployee employee = new Employee(1001, \"Jerry\", 23, 6000);\r\n\r\n\t\tFunction&lt;Employee,String> func1 = e -> e.getName();\r\n\t\tSystem.out.println(func1.apply(employee));\r\n\r\n\t\tSystem.out.println(\"***************************\");\r\n\r\n\t\tFunction&lt;Employee,String> func2 = Employee::getName;\r\n\t\tSystem.out.println(func2.apply(employee));\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P673;\r\n\r\nimport org.junit.jupiter.api.Test;\r\n\r\nimport java.util.Arrays;\r\nimport java.util.function.BiFunction;\r\nimport java.util.function.Function;\r\nimport java.util.function.Supplier;\r\n\r\n\/**\r\n * \u4e00\u3001\u6784\u9020\u5668\u5f15\u7528\r\n *      \u548c\u65b9\u6cd5\u5f15\u7528\u7c7b\u4f3c\uff0c\u51fd\u6570\u5f0f\u63a5\u53e3\u7684\u62bd\u8c61\u65b9\u6cd5\u7684\u5f62\u53c2\u5217\u8868\u548c\u6784\u9020\u5668\u7684\u5f62\u53c2\u5217\u8868\u4e00\u81f4\u3002\r\n *      \u62bd\u8c61\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5373\u4e3a\u6784\u9020\u5668\u6240\u5c5e\u7684\u7c7b\r\n *\r\n * \u4e8c\u3001\u6570\u7ec4\u5f15\u7528\r\n *      \u5927\u5bb6\u53ef\u4ee5\u628a\u6570\u7ec4\u770b\u505a\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u7c7b\uff0c\u5219\u5199\u6cd5\u4e0e\u6784\u9020\u5668\u5f15\u7528\u4e00\u81f4\u3002\r\n *\r\n *\r\n * Created by shkstart\r\n *\/\r\npublic class ConstructorRefTest {\r\n\t\/\/\u6784\u9020\u5668\u5f15\u7528\r\n    \/\/Supplier\u4e2d\u7684T get()\r\n    \/\/Employee\u7684\u7a7a\u53c2\u6784\u9020\u5668\uff1aEmployee()\r\n    @Test\r\n    public void test1(){\r\n\r\n        Supplier&lt;Employee> sup = new Supplier&lt;Employee>() {\r\n            @Override\r\n            public Employee get() {\r\n                return new Employee();\r\n            }\r\n        };\r\n\r\n        System.out.println(\"*******************************\");\r\n\r\n        Supplier&lt;Employee> sup1 = () -> new Employee();\r\n        System.out.println(sup1.get());\r\n\r\n        System.out.println(\"*******************************\");\r\n\r\n        Supplier&lt;Employee> sup2 = Employee :: new;\r\n        System.out.println(sup2.get());\r\n\t}\r\n\r\n\t\/\/Function\u4e2d\u7684R apply(T t)\r\n    @Test\r\n    public void test2(){\r\n        Function&lt;Integer,Employee> func1 = id -> new Employee(id);\r\n        Employee employee = func1.apply(1001);\r\n        System.out.println(employee);\r\n\r\n        System.out.println(\"*******************************\");\r\n\r\n        Function&lt;Integer,Employee> func2 = Employee :: new;\r\n        Employee employee1 = func2.apply(1002);\r\n        System.out.println(employee1);\r\n    }\r\n\r\n\t\/\/BiFunction\u4e2d\u7684R apply(T t,U u)\r\n    @Test\r\n    public void test3(){\r\n        BiFunction&lt;Integer,String,Employee> func1 = (id,name) -> new Employee(id,name);\r\n        System.out.println(func1.apply(1001,\"Tom\"));\r\n\r\n        System.out.println(\"*******************************\");\r\n\r\n        BiFunction&lt;Integer,String,Employee> func2 = Employee :: new;\r\n        System.out.println(func2.apply(1002,\"Tom\"));\r\n\t}\r\n\r\n\t\/\/\u6570\u7ec4\u5f15\u7528\r\n    \/\/Function\u4e2d\u7684R apply(T t)\r\n    @Test\r\n    public void test4(){\r\n        Function&lt;Integer,String&#91;]> func1 = length -> new String&#91;length];\r\n        String&#91;] arr1 = func1.apply(5);\r\n        System.out.println(Arrays.toString(arr1));\r\n\r\n        Function&lt;Integer,String&#91;]> func2 = String&#91;] :: new;\r\n        String&#91;] arr2 = func2.apply(10);\r\n        System.out.println(Arrays.toString(arr2));\r\n\r\n    }\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P673;\r\n\r\n\/**\r\n * @author shkstart \u90ae\u7bb1\uff1ashkstart@126.com\r\n *\/\r\npublic class Employee {\r\n\r\n\tprivate int id;\r\n\tprivate String name;\r\n\tprivate int age;\r\n\tprivate double salary;\r\n\r\n\tpublic int getId() {\r\n\t\treturn id;\r\n\t}\r\n\r\n\tpublic void setId(int id) {\r\n\t\tthis.id = id;\r\n\t}\r\n\r\n\tpublic String getName() {\r\n\t\treturn name;\r\n\t}\r\n\r\n\tpublic void setName(String name) {\r\n\t\tthis.name = name;\r\n\t}\r\n\r\n\tpublic int getAge() {\r\n\t\treturn age;\r\n\t}\r\n\r\n\tpublic void setAge(int age) {\r\n\t\tthis.age = age;\r\n\t}\r\n\r\n\tpublic double getSalary() {\r\n\t\treturn salary;\r\n\t}\r\n\r\n\tpublic void setSalary(double salary) {\r\n\t\tthis.salary = salary;\r\n\t}\r\n\r\n\tpublic Employee() {\r\n\t\tSystem.out.println(\"Employee().....\");\r\n\t}\r\n\r\n\tpublic Employee(int id) {\r\n\r\n\t\tthis.id = id;\r\n\t\tSystem.out.println(\"Employee(int id).....\");\r\n\t}\r\n\r\n\tpublic Employee(int id, String name) {\r\n\t\tthis.id = id;\r\n\t\tthis.name = name;\r\n\t}\r\n\r\n\tpublic Employee(int id, String name, int age, double salary) {\r\n\r\n\t\tthis.id = id;\r\n\t\tthis.name = name;\r\n\t\tthis.age = age;\r\n\t\tthis.salary = salary;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic String toString() {\r\n\t\treturn \"Employee{\" + \"id=\" + id + \", name='\" + name + '\\'' + \", age=\" + age + \", salary=\" + salary + '}';\r\n\t}\r\n\r\n\t@Override\r\n\tpublic boolean equals(Object o) {\r\n\t\tif (this == o)\r\n\t\t\treturn true;\r\n\t\tif (o == null || getClass() != o.getClass())\r\n\t\t\treturn false;\r\n\r\n\t\tEmployee employee = (Employee) o;\r\n\r\n\t\tif (id != employee.id)\r\n\t\t\treturn false;\r\n\t\tif (age != employee.age)\r\n\t\t\treturn false;\r\n\t\tif (Double.compare(employee.salary, salary) != 0)\r\n\t\t\treturn false;\r\n\t\treturn name != null ? name.equals(employee.name) : employee.name == null;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic int hashCode() {\r\n\t\tint result;\r\n\t\tlong temp;\r\n\t\tresult = id;\r\n\t\tresult = 31 * result + (name != null ? name.hashCode() : 0);\r\n\t\tresult = 31 * result + age;\r\n\t\ttemp = Double.doubleToLongBits(salary);\r\n\t\tresult = 31 * result + (int) (temp ^ (temp >>> 32));\r\n\t\treturn result;\r\n\t}\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P673;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\/**\r\n * \u63d0\u4f9b\u7528\u4e8e\u6d4b\u8bd5\u7684\u6570\u636e\r\n * \r\n * @author shkstart \u90ae\u7bb1\uff1ashkstart@126.com\r\n *\r\n *\/\r\npublic class EmployeeData {\r\n\t\r\n\tpublic static List&lt;Employee> getEmployees(){\r\n\t\tList&lt;Employee> list = new ArrayList&lt;>();\r\n\t\t\r\n\t\tlist.add(new Employee(1001, \"\u9a6c\u5316\u817e\", 34, 6000.38));\r\n\t\tlist.add(new Employee(1002, \"\u9a6c\u4e91\", 12, 9876.12));\r\n\t\tlist.add(new Employee(1003, \"\u5218\u5f3a\u4e1c\", 33, 3000.82));\r\n\t\tlist.add(new Employee(1004, \"\u96f7\u519b\", 26, 7657.37));\r\n\t\tlist.add(new Employee(1005, \"\u674e\u5f66\u5b8f\", 65, 5555.32));\r\n\t\tlist.add(new Employee(1006, \"\u6bd4\u5c14\u76d6\u8328\", 42, 9500.43));\r\n\t\tlist.add(new Employee(1007, \"\u4efb\u6b63\u975e\", 26, 4333.32));\r\n\t\tlist.add(new Employee(1008, \"\u624e\u514b\u4f2f\u683c\", 35, 2500.32));\r\n\t\t\r\n\t\treturn list;\r\n\t}\r\n\t\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-1433","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1433"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1433"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1433\/revisions"}],"predecessor-version":[{"id":1434,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1433\/revisions\/1434"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}