The interface with one and only one abstract function is the functional interface. Using the functional interface we can create lamdba, but it can include static methods and default
First we look at the application of a simple lambba expression, that is, when creating a thread
//创建一个线程,将线程的名字打印出来 new Thread(()- System.out.println(Thread.currentThread().getName())).start(); Thread.currentThread().join(); //Thread-0
Look at the following example:
Comparator<Apple> byColor = new Comparator<Apple>() { @Override public int compare(Apple o1, Apple o2) { return o1.getColor().compareTo(o2.getColor()); } }; listApple.sort(byColor); Comparator<Apple> byCOlor2 = (o1,o2) -> o1.getColor().compareTo(o2.getColor());
It is not difficult to see that the syntax of lamdba is composed as follows:
Parameter function body
Just like o1 and o2 above are parameters, What follows is the function body (Apple a) -> a.getColor() (parameters) -> expression (parameters) ->{statements}
compareTo function introduction:
// 根据Unicode来判断,返回一个boolean 根据boolean来说明,这两个对象需不需要交换,其实我们可以不用这个CompareTo,我们可以自己写判断条件,只要是返回boolean就行 public int compareTo(String anotherString) { int len1 = value.length; int len2 = anotherString.value.length; int lim = Math.min(len1, len2); char v1[] = value; char v2[] = anotherString.value; int k = 0; while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; }
Predicate boolean test(T t)
Consumer accept(T t)
Function469df1b70b2914a3841e0404ec3e3d5d R apple(T,t)
Supplier T get()
Predicate: Pass in a variable and return a Boolean value
private static List<Apple> filterApple(List<Apple> list, Predicate<Apple> predicate){ List<Apple> lists = new ArrayList<>(); for (Apple apple:list){ if (predicate.test(apple)){ lists.add(apple); } } return lists; } //使用 List<Apple> list = filterApple(listApple, apple -> apple.getWeight() > 100); System.out.println(list); //我们发现在 filterApple 函数里面我们传入了两个参数,一个是list 一个是Predicate抽象函数式接口,我们调用这个抽象函数式接口,但是我们不实现它,我们在调用的时候实现它是根据重量来比较,当然我们在调用的时候也可以根据颜色来比较 /** 举一反三:Predicate是传入一个参数进行判断,BiPredicate是传入两个参数进行判断,例如下面的例子 */ private static List<Apple> filterAppleByBiPredicaTe(List<Apple> apples, BiPredicate<String, Long> predicate){ List<Apple> lists = new ArrayList() for(Apple apple:apples){ if (predicate.test(apple.getColor(),apple.getWeight())){ lists.add(apple) } } //从上面的例子上来看我们知道BiPredicate传入两个参数,根据颜色和重量来进行比较,我们在调用的时候可以实现者两个参数的比较规则 }
The Chinese word "Consumer" means consumer. Since it is consumed, there will be no return value
private static void printlnApple(List<Apple> list, Consumer<Apple> consumer){ if (list.size()>0){ for (Apple apple:list){ consumer.accept(apple); } } } //使用 printlnApple(listApple,apple -> System.out.println(apple.getColor())); /** 从上面我们可以看到 这个Consumer不会返回任何对象 举一反三: 上面有BiPredicate,那么我们这个Consumer有没有BiConsumer,答案肯定是有的,下面我们来看下BiConsumer的用法 */ private static void printGreenAppleByBiConsumer(List<Apple> apples, Consumer<Apple, String> consumer){ if(apples.size()>0){ for(Apple apple:apples){ consumer.accept(apple, apple.getColor()) } } } //使用 printGreenAppleByBiConsumer(list,(apple, color)->{ if(apple.getWeight()>100&&color.equal("green")){ System.out.println(apple); } }) //学习玩上面的例子 我再提一个 LongConsumer,从字面上看,我们知道,它接收的是一个Long类型的,其他的并没什么区别,这里我们就不多啰嗦了
Function: Function, we know that functions all have outputs. What this means is that a parameter is passed in and a parameter is returned. Let’s take a look at how to use this. Function
private static Long getWeightList(Apple apple, Function<Apple,Long> function) { return function.apply(apple); } //使用 Long green = getWeightList(new Apple("green", 120), Apple::getWeight); System.out.println(green); //在这里我们可以看到,Function函数的apply抽象方法接收一个值,返回一个Long类型的值 //根据上面的经验,想必大家也已经知道了还有 BiFunction 这个函数式接口,没错它只是传进去的参数有两个而已,还是返回一个结果。除此之外还有呢,大家别着急,还有 IntFunction DoubleFunction,这些有什么作用呢,其实可以看成是Function的具体情况 IntFuntion也就是 apply里面的参数是Int类型的 DoubleFunction也就是apply的参数是double类型的,大家再看一下,是不是非常非常的简单呢
Supplier: This word means to provide, that is, to return data to you. This function interface will return data to you and does not require you to pass in data
private static Apple getApple(Supplier<Apple> supplier){ return supplier.get(); } //使用 Apple green1 = getApple(() -> new Apple("green", 150)); System.out.println(green1); //有人肯定再想了,这样创建对象那不是脱裤子放屁吗 多此一举,这里只是举一个小例子,在实际开发中肯定不会这样
1, inference through the static method of a class
2 , use the instance method of the object to infer
private static void printstr(Consumer<String> consumer, String str){ consumer.accept(str); } //使用 Consumer<String> consumer = a -> System.out.println(a); printstr(consumer,"hello world"); //写成 Consumer<String> consumer = System.out::println printstr(consumer,"hello world"); //思考我们为什么可以写成这样呢,我们看下println的源码 // public final static PrintStream out = null;,在源码里面我们可以看到PrintStream是一个静态的类,而println是这个静态类下面的方法,因此可以做函数推导 public void println(String x) { //刚好是接收一个参数,没有返回 synchronized (this) { print(x); newLine(); } } ```再举个例子 ```java //将字符串转换成数字 int value = Integer.parseInt("123") Function<String, Integer> stringFunction = Integer::parseInt; Integer result = stringFunction.apply("123"); System.out.println(result) /** 很多人看到这里就很纳闷了,怎么冒出一个 Function来了呢,这就是类方法推导,parseInt是Integer的类方法它需要传入一个数,然后输出一个数,这样的模式就是前面我们介绍的Function函数式接口,接着再来看一个例子 */ String string = new String("hello"); Function<Integer, Character> f = string::charAt; Character c = f3.apply(4); System.out.println(c);
Constructor derivation:
Supplier<String> supplier = String::new String s = supplier.get(); System.out.print(s) //但是Apple是两个构造参数的,怎么办呢,还是有办法的 BiFunction<String, Long,Apple> appleFunction = Apple::new; Apple apple = appleFunction.apply("red",100L); //如果有更多的参数呢,在这里我们就可以自定义函数式接口 public interface CMyFuction<T,U,W,R>{ R get(T t,U u, W w,R,r); } CMyFuction<String,String, Long> cmyfunction = Apple::new; Apple apple = cmyfunction.get("green","large",120); //ok 这样自定义的函数式接口就完成了
Example:sort classification and function Equation derivation
//定义一个集合 static List<Apple> listApple = Arrays.asList( new Apple("green",122), new Apple("red", 34), new Apple("black",135), new Apple("green",114), new Apple("yellow",54), new Apple("yellow",94)); //以前我们排序的时候只知道调用 sort方法例如 listAplle.sort() //但是我们不知道它怎么操作的,下面我们可以自定义排序的规则 list.sort() //写道这里系统提示参数可以为Comparator,我们接着写 list.sort(new Comparator<Apple>{ public int compare(Apple o1, Apple o2){ return o1.getColor().compareTo(o2.getColor()); } }) //从上面的里例子来看我们是根据apple的颜色来进行比较的,当然我们也可以根据apple的weight来进行比较.接着我们进行函数推导 list.sort(Comparator.comparing(Apple::getColor)) //就这么一句完全代替了我们上面的功能,很明显简化了代码
The above is the detailed content of How to express the syntax of functional derivation in Java8 Lamdba. For more information, please follow other related articles on the PHP Chinese website!