java中JApple用法_Jjava8 Lambda 神操作

1 public classLambda {2

3 @FunctionalInterface4 public interfaceAddInter {5 void add(int x, longy);6 }7

8 public interface AddInterTow extendsAddInter {9 void add(int x, longy);10 }11

12 ///Predicate 传入apple类型的参数

13 private static List findApple1(List appleList, PredicateapplePredicate) {14 ArrayList newArrayList =Lists.newArrayList();15 for(Apple apple : appleList) {16 if(applePredicate.test(apple)) {17 newArrayList.add(apple);18 }19 }20 returnnewArrayList;21 }22

23 ///Predicate 传入Long类型的参数 一个参数调用括号可以省略

24 private static List findApple2(List appleList, PredicateapplePredicate) {25 ArrayList newArrayList =Lists.newArrayList();26 for(Apple apple : appleList) {27 if(applePredicate.test(apple.getWeight())) {28 newArrayList.add(apple);29 }30 }31 returnnewArrayList;32 }33

34 ///两个参数的

35 private static List findApple3(List appleList, BiPredicateapplePredicate) {36 ArrayList newArrayList =Lists.newArrayList();37 for(Apple apple : appleList) {38 if(applePredicate.test(apple.getColor(), apple.getWeight())) {39 newArrayList.add(apple);40 }41 }42 returnnewArrayList;43 }44

45 ///一个参数的Consumer,accept方法没有返回值 具体干什么你来定

46 private static void findApple4(List appleList, ConsumerappleConsumer) {47 for(Apple apple : appleList) {48 appleConsumer.accept(apple);49 }50 }51 ///两个参数的

52 private static void findApple5(String d,List appleList, BiConsumerappleConsumer) {53 for(Apple apple : appleList) {54 appleConsumer.accept(apple,d);55 }56 }57 private static String findApple6(Apple apple, FunctionstringFunction) {58 returnstringFunction.apply(apple);59 }60

61 public static voidmain(String[] args) {62 ArrayList list = Lists.newArrayList(new Apple("gree", 150L), new Apple("red", 200L), new Apple("blue", 300L));63 ///Predicate一个参数 || BiPredicate两个参数 || IntPredicate int参数的

64 List apple1 = findApple1(list, (apple) -> apple.getColor().equals("red"));65 System.out.println(JSON.toJSONString(apple1));66 List apple2 = findApple2(list, num -> num > 150L);67 System.out.println(JSON.toJSONString(apple2));68 List 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 apple7 = i->i*20.0d;81 System.out.println(apple7.apply(20));82 //Predicate || Function ||Supplier || Consumer 常用的几个类

83

84 System.out.println("===================================>推导==================");85 Consumer 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 stringIntegerSFunction =Integer::parseInt;94 Integer integer = stringIntegerSFunction.apply("1234");95 System.out.println(integer);96

97

98 BiFunction biFunction = Apple::new;99 Apple biapple = biFunction.apply("biapple", 1000L);100 System.out.println(biapple);101 }102

103 public static void consumer(Consumerconsumer,T t){104 consumer.accept(t);105 consumer.accept(t);106 }107 public static voidLambdaRun() {108 SFunction stringObjectSFunction = (String s) ->s.length();109 System.out.println(stringObjectSFunction.apply("大傻大傻大傻大傻"));110

111 Predicate applePredicate = (apple -> apple.getColor().equals("red1"));112 System.out.println(applePredicate.test(new Apple("red", 123L)));113

114 Supplier appleSupplier = Apple::new;115 System.out.println(appleSupplier.get());116

117 Runnable run = newRunnable() {118 @Override119 public voidrun() {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 voidstartRunnable(Runnable runnable) {130 runnable.run();131 }132 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值