浅谈lambda表达式

lambda表达式有两种使用方式,一种是作为方法体中元素,将表达式写在方法体中进行调用,一种是作为方法的参数进行调用,下面是几个简单的例子

 1 package LambdaTest;
 2 
 3 import java.beans.Customizer;
 4 import java.util.function.Consumer;
 5 import java.util.function.Function;
 6 import java.util.function.Predicate;
 7 import java.util.function.Supplier;
 8 
 9 import javax.xml.ws.Provider;
10 
11 import CollectTest.add;
12 
13 public class Lambda01    {
14 
15     
16     //断言型
17     public static void test01(int a)
18     {
19         Predicate<Integer> p = num -> num>10 ;
20         System.out.println(p.test(a));
21     }
22     
23     //消费型
24     public static void test02(String a)
25     {
26         Consumer<String> c = o -> System.out.println(o);
27         c.accept(a);
28     }
29     
30     //生产型
31     public static String test03() 
32     {
33         Supplier<String> su = () -> "许静歌" ;
34         return su.get();
35     }
36     
37     //正常型 
38     public static int test04(int c,int b)
39     {
40         add a = (n1,n2) -> n1+n2;
41         return a.res(c, b);
42     }
43     
44     //接口作为方法的参数
45     public static String test05(Function<String, String> fun,String str) 
46     {
47          return fun.apply(str);     
48     }
49     
50     public static void test06(Consumer<String> con,String str)
51     {
52         con.accept(str);
53     }
54 
55     public static void main(String[] args) {
56     
57         test01(11);
58         test02("zs");
59         System.out.println(test03());
60         System.out.println(test04(1,2));
61         System.out.println(test05((s) -> s.toUpperCase(),"aa"));
62         test06(o -> System.out.println(o),"你是大美女");
63     }
64 
65     
66 }
原文地址:https://www.cnblogs.com/aierben/p/14483517.html