lambada表达式理解

lambada表达式是替换匿名内部类的函数式接口实现类

函数式接口 只有一个抽象方法的接口(可以有默认方法和静态方法),通常带有@FunctionalInterface注解

常用函数式接口

  1. Predicate / ˈpredɪkət/ 断言型函数式接口

    @FunctionalInterface
    public interface Predicate<T> {
    
        /**
         * ——接收`T`对象并返回`boolean`
         */
        boolean test(T t);
    }
    
  2. Consumer 消费型函数式接口

    @FunctionalInterface
    public interface Consumer<T> {
    
        /**
         * 接收T对象,不返回值
         */
        void accept(T t);
    }
    
  3. Function<T, R> 功能型函数式接口

    @FunctionalInterface
    public interface Function<T, R> {
    
        /**
         * ——接收`T`对象,返回`R`对象
         */
        R apply(T t);
    }
    
    
  4. Supplier<T> 供应型函数式接口

    @FunctionalInterface
    public interface Supplier<T> {
    
        /**
         *  ——返回`T`对象(例如工厂),不接收值
         */
        T get();
    }
    

我们只看接口的方法

函数式接口入参返回对象
供给型函数式接口 T get();T
消费型函数式接口 void accept(T t);T
断言型函数式接口 boolean test(T t);Tboolean
功能型函数式接口 R apply(T t);TR

从上来看,断言型接口是功能型接口的子集。断言型接口是函数式接口的一种。

供给型接口示例

class StringSupplier{

    public static String get() {
        return "我很好";
    }
    public  String get2() {
        return "我很好2";
    }
}
/**
 * public interface Supplier<T> {
 *  T get();
 *}
 **/
public class Supplier1 {
    public static void main(String[] args) {
        //匿名内部类写法
        Supplier c = new Supplier() {
            @Override
            public Object get() {
                return "我很好!";
            }
        };
        System.out.println("c.get() = " + c.get());//c.get() = 我很好!

        //lambada 写法
        //ambda表达式的语法由方法 参数列表、箭头符号->和函数体组成。
        Supplier c2 = ()->{
            return "我很好!";
        };
        System.out.println("c2.get() = " + c2.get());//c2.get() = 我很好!

        //方法引用 如果方法的入参和返回值类型一样可以用方法引用。
        //Lambda表达式让我们自己重新写方法实现,方法引用是使用已经存在的实现。
        // 类::静态方法引用
        Supplier s = StringSupplier::get;
        System.out.println("s.get() = " + s.get());//s.get() = 我很好
        // 对象::普通方法引用
        Supplier s2 = new StringSupplier()::get2;
        System.out.println("s2.get() = " + s2.get());//s2.get() = 我很好2
        //类::new
        Supplier constructor=StringSupplier::new;//引用构造方法
        StringSupplier o = (StringSupplier)constructor.get();//调用构造方法,产生对象
        System.out.println("constructor = " + constructor.get());//constructor = com.axa.aam.service.StringSupplier@254989ff

    }
}

消费型函数式接口示例

class StringConsumer<String>{
    public  void get(String s){
        System.out.println("s = " + s);
    }
}

/**
 * public interface Consumer<T> {
 *      void accept(T t);
 *}
 **/
public class Consumer2 {
    public static void main(String[] args) {
        //匿名内部类写法
        Consumer c = new Consumer<String>() {
            @Override
            public void accept(String o) {
                System.out.println("o = " + o);
            }
        };

        c.accept("我很好!");// c.get() = 我很好!

        //lambada 写法
        //ambda表达式的语法由方法 参数列表、箭头符号->和函数体组成。
        Consumer c2 = (o)->{
            System.out.println("o = " + o);
        };
        c2.accept("我很好!"); //c2.get() = 我很好!

        //方法引用 如果方法的入参和返回值类型一样可以用方法引用。
        //Lambda表达式让我们自己重新写方法实现,方法引用是使用已经存在的实现。
        // 对象::普通方法引用
        Consumer s2 = new StringConsumer()::get;
        s2.accept("我很好?");

    }
}

断言型函数式接口示例

class StringPredicate<String>{
    public  boolean get(String s){
        return s==null;
    }
}

/**
 * public interface Predicate<T> {
 *
 *      boolean test(T t);
 *}
 **/
public class Predicate3 {
    public static void main(String[] args) {
        //匿名内部类写法
        Predicate c = new Predicate<String>() {
            @Override
            public boolean test(String o) {
                return o==null;
            }
        };

        System.out.println(c.test("我很好!"));// c.test() = false

        //lambada 写法
        //ambda表达式的语法由方法 参数列表、箭头符号->和函数体组成。
        Predicate c2 = (o)->{
            return o==null;
        };
        System.out.println(c2.test("我很好!")); //c2.test() = false

        //方法引用 如果方法的入参和返回值类型一样可以用方法引用。
        //Lambda表达式让我们自己重新写方法实现,方法引用是使用已经存在的实现。
        // 对象::普通方法引用
        Predicate s2 = new StringPredicate()::get;
        System.out.println(s2.test("我很好?")); //s2.test() false
    }

功能型函数式接口示例

class StringFunction<Integer,String>{
    
    public java.lang.String get(Integer i){
        return i.toString();
    }
    public StringFunction(Integer q) {
        System.out.println("我是构造方法");
    }
    public StringFunction() {
    }
}

/**
 * public interface Function<T, R> {
 *  R apply(T t);
 *}
 **/
public class Function4 {
    public static void main(String[] args) {
        //匿名内部类写法
        Function c=new Function<Integer,String>() {
            @Override
            public String apply(Integer o) {
                return o.toString();
            }
        };

        System.out.println(c.apply(5));// c.apply() = 5

        //lambada 写法
        //ambda表达式的语法由方法 参数列表、箭头符号->和函数体组成。
        Function c2 = (o)->{
            return o.toString();
        };
        System.out.println(c2.apply(6)); //c2.apply() = 6

        //方法引用 如果方法的入参和返回值类型一样可以用方法引用。
        //Lambda表达式让我们自己重新写方法实现,方法引用是使用已经存在的实现。
        // 对象::普通方法引用
        Function s2 = new StringFunction()::get;
        System.out.println(s2.apply(7)); //s2.apply() 7

        Comparator<String> stringComparator = String::compareTo;
        //类::new 构造器引用
        Function<Integer, StringFunction> s3 = StringFunction::new; //引用构造方法
        StringFunction apply = s3.apply(8);  //调用构造方法,产生对象
        System.out.println("apply = " + apply);//apply = com.axa.aam.service.StringFunction@179d3b25
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值