Jjava8 Lambda 神操作
程序员文章站
2023-10-29 12:17:52
1 public class Lambda { 2 3 @FunctionalInterface 4 public interface AddInter { 5 void add(int x, long y); 6 } 7 8 public interface AddInterTow extends... ......
1 public class lambda { 2 3 @functionalinterface 4 public interface addinter { 5 void add(int x, long y); 6 } 7 8 public interface addintertow extends addinter { 9 void add(int x, long y); 10 } 11 12 ///predicate 传入apple类型的参数 13 private static list<apple> findapple1(list<apple> applelist, predicate<apple> applepredicate) { 14 arraylist<apple> newarraylist = lists.newarraylist(); 15 for (apple apple : applelist) { 16 if (applepredicate.test(apple)) { 17 newarraylist.add(apple); 18 } 19 } 20 return newarraylist; 21 } 22 23 ///predicate 传入long类型的参数 一个参数调用括号可以省略 24 private static list<apple> findapple2(list<apple> applelist, predicate<long> applepredicate) { 25 arraylist<apple> newarraylist = lists.newarraylist(); 26 for (apple apple : applelist) { 27 if (applepredicate.test(apple.getweight())) { 28 newarraylist.add(apple); 29 } 30 } 31 return newarraylist; 32 } 33 34 ///两个参数的 35 private static list<apple> findapple3(list<apple> applelist, bipredicate<string, long> applepredicate) { 36 arraylist<apple> newarraylist = lists.newarraylist(); 37 for (apple apple : applelist) { 38 if (applepredicate.test(apple.getcolor(), apple.getweight())) { 39 newarraylist.add(apple); 40 } 41 } 42 return newarraylist; 43 } 44 45 ///一个参数的consumer,accept方法没有返回值 具体干什么你来定 46 private static void findapple4(list<apple> applelist, consumer<apple> appleconsumer) { 47 for (apple apple : applelist) { 48 appleconsumer.accept(apple); 49 } 50 } 51 ///两个参数的 52 private static void findapple5(string d,list<apple> applelist, biconsumer<apple,string> appleconsumer) { 53 for (apple apple : applelist) { 54 appleconsumer.accept(apple,d); 55 } 56 } 57 private static string findapple6(apple apple, function<apple,string> stringfunction) { 58 return stringfunction.apply(apple); 59 } 60 61 public static void main(string[] args) { 62 arraylist<apple> list = lists.newarraylist(new apple("gree", 150l), new apple("red", 200l), new apple("blue", 300l)); 63 ///predicate一个参数 || bipredicate两个参数 || intpredicate int参数的 64 list<apple> apple1 = findapple1(list, (apple) -> apple.getcolor().equals("red")); 65 system.out.println(json.tojsonstring(apple1)); 66 list<apple> apple2 = findapple2(list, num -> num > 150l); 67 system.out.println(json.tojsonstring(apple2)); 68 list<apple> apple3 = findapple3(list, (a, b) -> a.equals("red") && b >= 200); 69 system.out.println(apple3); 70 system.out.println("==========================================>"); 71 72 ///consumer 一个参数的 73 findapple4(list, a -> system.out.println("{"+a.getcolor()+":"+a.getweight()+"}")); 74 findapple5("consumer",list,(a,b)-> system.out.println(a.getcolor()+":"+a.getweight()+":"+b)); 75 system.out.println("=======================================>"); 76 ///function 传入一个值 返回一个值 77 string apple6 = findapple6(new apple("apple", 3000l), (x) -> x.tostring()); 78 system.out.println(apple6); 79 80 intfunction<double> apple7 = i->i*20.0d; 81 system.out.println(apple7.apply(20)); 82 //predicate || function ||supplier || consumer 常用的几个类 83 84 system.out.println("===================================>推导=================="); 85 consumer<string> consumerstring = (s -> system.out.println(s)); 86 consumer(consumerstring,"坚持"); 87 consumer(s-> system.out.println(s),"hellword consunmer"); 88 consumer(system.out::println,"system.out.pring"); 89 90 integer aaa = integer.parseint("12345"); 91 92 93 sfunction<string, integer> stringintegersfunction = integer::parseint; 94 integer integer = stringintegersfunction.apply("1234"); 95 system.out.println(integer); 96 97 98 bifunction<string,long,apple> bifunction = apple::new; 99 apple biapple = bifunction.apply("biapple", 1000l); 100 system.out.println(biapple); 101 } 102 103 public static <t> void consumer(consumer<t> consumer,t t){ 104 consumer.accept(t); 105 consumer.accept(t); 106 } 107 public static void lambdarun() { 108 sfunction<string, object> stringobjectsfunction = (string s) -> s.length(); 109 system.out.println(stringobjectsfunction.apply("大傻大傻大傻大傻")); 110 111 predicate<apple> applepredicate = (apple -> apple.getcolor().equals("red1")); 112 system.out.println(applepredicate.test(new apple("red", 123l))); 113 114 supplier<apple> applesupplier = apple::new; 115 system.out.println(applesupplier.get()); 116 117 runnable run = new runnable() { 118 @override 119 public void run() { 120 system.out.println("hello word"); 121 } 122 }; 123 runnable runnable = () -> system.out.println("hello word"); 124 startrunnable(runnable); 125 startrunnable(run); 126 startrunnable(() -> system.out.println("hello")); 127 } 128 129 public static void startrunnable(runnable runnable) { 130 runnable.run(); 131 } 132 }
上一篇: 木卯先生的笔记---Object类
下一篇: 减肥期间吃辣条影响吗,吃辣椒的注意事项