Jjava8 Lambda 神操作
Posted 成长
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jjava8 Lambda 神操作相关的知识,希望对你有一定的参考价值。
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 }
以上是关于Jjava8 Lambda 神操作的主要内容,如果未能解决你的问题,请参考以下文章