Java Lambda表达式ConsumerFunctionBinaryOperatorPredicateSupplierUnaryOperator接口

Posted 11biscuits

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Lambda表达式ConsumerFunctionBinaryOperatorPredicateSupplierUnaryOperator接口相关的知识,希望对你有一定的参考价值。

Lambda表达式、

Main.java

package Lambda表达式;

public class Main {
    public  String message="你好";
    public static void main(String[]argv){
        /*
        功能性接口:只有一个抽象方法
        */
        greet(new ConsolePrinter());
        //使用匿名类
        greet(new Printer() {
            @Override
            public void print(String message) {
                System.out.println("使用匿名类"+message);
            }
        });
        //lambda表达式
        greet((String message) -> {
            System.out.println("Lambda表达式"+message);
        });
        //定义lambda表达式
        Printer lambdaPrinter=message -> {System.out.println(message);};
        lambdaPrinter.print("helloworld");
        //lambda执行体中的this为当前所执行lambda表达式的对象实体

        //使用方法引用
        greet(message -> System.out.println(message));
        greet(System.out::println);
        //自定义
        greet(message -> print(message));
        greet(Main::print);
        //静态方法
        //gree(类名::方法名)
        //实体方法
        //gree(对象变量名::方法名)
    }
    public static void greet(Printer printer){
        printer.print("Hello world");
    }
    public static void print(String message){
        System.out.println("方法引用"+message);
    }
}

Printer.java

package Lambda表达式;

public interface Printer {
    void print(String message);
}

ConsolePrinter.java

package Lambda表达式;

public class ConsolePrinter implements Printer{

    @Override
    public void print(String message) {
        System.out.println(message);
    }
}

Consumer接口

package Lambda表达式.Consumer接口;

import java.util.List;
import java.util.function.Consumer;

public class Main {
    public static int sum=0;
    public static void main(String[]argv){
        List<Integer>list= List.of(3,4,5,6);
        //遍历
        for(Integer num:list){
            System.out.println(num);
        }
        //使用Consumer接口
        list.forEach((Integer item)->{
            sum+=item;
            System.out.println(item);
        });
        System.out.println("sum="+sum);
        //定义Consumer接口:只接受参数没有返回值
        Consumer<Integer> print=(Integer item)->System.out.println("print"+item);
        list.forEach(print);
        /*消费者链*/
        Consumer<Integer> print1=item->System.out.println("print1+"+item);
        list.forEach(print.andThen(print1));
//        print3
//        print1+3
//        print4
//        print1+4
//        print5
//        print1+5
//        print6
//        print1+6
    }
}

Function接口

package Lambda表达式.Function接口;

import java.util.function.Function;

public class Main {
    public static void main(String[]argv){
        Function<String,Integer> map=(String str)->{return str.length();};
        var num=map.apply("hello");
        System.out.println(num);
        /*有趣的实例*/
        Function<String,String>replaceColon=str->str.replace(":","=");
        Function<String,String>addBraces=str->"{"+str+"}";
        var result=replaceColon.
                            andThen(addBraces)
                            .apply("key:value");
        System.out.println(result);//{key=value}
        //以相反的顺序组合函数
        System.out.println(addBraces.compose(replaceColon).apply("key:value"));
    }
}

BinaryOperator接口

package Lambda表达式.BinaryOperator接口;

import java.util.function.BinaryOperator;
import java.util.function.Function;

public class Main {
    public static void main(String[]argv){
        BinaryOperator<Integer>add=(a,b)->a+b;
        Function<Integer,Integer> square= a->a*a;
        System.out.println(add.andThen(square).apply(1,2));
        //(1+2)^2
    }
}

Predicate接口

package Lambda表达式.Predicate接口;

import java.util.function.Predicate;

public class Main {
    public static void main(String[]argv){
        Predicate<String>isLongerThan5=str->str.length()>5;
        System.out.println(isLongerThan5.test("hcv"));//false

        //组合谓词接口
        Predicate<String>hasLeftBrace=str->str.startsWith("{");
        Predicate<String>hasRightBrace=str->str.endsWith("}");
        //and
        Predicate<String>hasLeftRightBrace=hasLeftBrace.and(hasRightBrace);
        System.out.println(hasLeftRightBrace.test("{hello}"));//true
        //or
        Predicate<String>hasLeftOrRightBrace=hasLeftBrace.or(hasRightBrace);
        System.out.println(hasLeftOrRightBrace.test("hello}"));//true
        //取反
        hasLeftBrace.negate();
    }
}

Supplier接口

package Lambda表达式.Supplier接口;

import java.util.function.Supplier;

public class Main {
    public static void main(String[]argv){
        Supplier<Double> getRandom=()->{return Math.random();};
        var random=getRandom.get();
        System.out.println(random);
    }
}

UnaryOperator接口

package Lambda表达式.UnaryOperator接口;

import java.util.function.UnaryOperator;

public class Main {
    public static void main(String[]argv){
        UnaryOperator<Integer> square=n->n*n;
        UnaryOperator<Integer> increment=n->n+1;
        var result=increment.andThen(square).apply(1);
        System.out.println(result);//4
        //(1+1)*(1+1)=4
    }
}

以上是关于Java Lambda表达式ConsumerFunctionBinaryOperatorPredicateSupplierUnaryOperator接口的主要内容,如果未能解决你的问题,请参考以下文章

java中的lambda表达式是啥?

Lambda表达式的Java表达式

java拓展----探秘Lambda 表达式

JAVA 8 Lambda表达式-Lambda Expressions

java lambda表达式速度快吗

:Java之lambda表达式