Lamdba表达式和函数表达式(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jeekmary/article/details/81484864

Lamdba想必很多人都知道,但是实际上很少有人真的能用起来,其实这个功能还是很方便的,在我们日常开发中都是很实用的。

条件,要求jdk的版本是 8.0或者更高的版本,现在jdk9.0都出来了,这个功能我们必须要掌握,方便我们的开发,减少了代码量


一般形式

(这里写参数) -> {这里写函数体}

当然如果参数和函数体很少,就可以不写外面的区域符号,例如persion -> "100"这个看不懂没关系,我只是说明下有这种情况

1, 我们自己定义一个借口,然后制作一个函数式接口

    @FunctionalInterface
    interface WrapUser{
       void insert(User user);
    }

2,定义一个User类,这个就不多说了

3,使用:

  User user = new User("hongbiao", 25);
        WrapUser wu  = User -> System.out.print(user.getName()+"--"+user.getAge());
        wu.insert(user);

解释下,这段代码,第一行是创建一个对象,第二行是实例化这个接口,实例化这个接口就要实现这个接口里面的所有抽象的方法,返回的是一个接口对象 wu, 最后一步就是调用这个接口里面的方法。
主要是第二句 User表示的是需要的参数类型,-> 后面的是函数体,因为这里只有一句话所以省略了花括号,也可以这样写
WrapUser wu = (User) -> {System.out.print(user.getName()+"--"+user.getAge());};

函数体内调用方法

(这里写参数) -> {function()}

1, 首先要确认接口中是否有返回值,如果有返回值,这单独函数体内必须有返回值,没有返回值会报错,如果接口中没有返回值,这在函数体内无论有没有返回值都可以。

2,实例

接口,无返回值

    @FunctionalInterface
    interface WrapUser{
       void insert(User user);
    }

有返回值的方法和无返回值的方法

    static void delet(){
        System.out.print("delete---");
    }
    static int add(){
        System.out.print("delete---");
        return 1;
    }

使用,这样一来,lamdba表达式调用函数,无论函数是否有返回值都不报错

        User user = new User("hongbiao", 25);
        WrapUser wu  = User ->  System.out.print(user.getName()+"--"+user.getAge());
        wu.insert(user);
        WrapUser wu2  = User ->  delet();
        wu.insert(user);
        WrapUser wu3  = User ->  add();
        wu.insert(user);

接口,有返回值 此时的接口是一个有返回值的接口

    @FunctionalInterface
    interface MapSet{
        int get();
    }

有返回值和无返回值的方法

    static int getAge(){
        return 25;
    }
    static void delet(){
        System.out.print("delete---");
    }

使用 这里写代码片

        MapSet map = () -> 100;
        map.get();

        MapSet map2 = () -> getAge();
        map.get();
        /**
         * 接口中需要返回值,而delete这个函数没有返回值,因此报错
         */
        MapSet map3 = () -> delet(); //这里是报错的
        map.get();

这里写图片描述

函数式接口,以上所说的各种lamdba,是不是所有的接口都可以这样写呢,当然不是,所谓的函数式编程是指一个有且只有一个抽象方法的接口,当然 用default和static修饰的方法除外,这两个关键字是java8 里面的新特性,例如我们看一下系统接口Runable这个接口,在接口上一行使用@FunctionalInterface注解表明这是一个函数式接口,那么我们就可以使用lamdba表达式来写。

package java.lang;

/**
 * The <code>Runnable</code> interface should be implemented by any
 * class whose instances are intended to be executed by a thread. The
 * class must define a method of no arguments called <code>run</code>.
 * <p>
 * This interface is designed to provide a common protocol for objects that
 * wish to execute code while they are active. For example,
 * <code>Runnable</code> is implemented by class <code>Thread</code>.
 * Being active simply means that a thread has been started and has not
 * yet been stopped.
 * <p>
 * In addition, <code>Runnable</code> provides the means for a class to be
 * active while not subclassing <code>Thread</code>. A class that implements
 * <code>Runnable</code> can run without subclassing <code>Thread</code>
 * by instantiating a <code>Thread</code> instance and passing itself in
 * as the target.  In most cases, the <code>Runnable</code> interface should
 * be used if you are only planning to override the <code>run()</code>
 * method and no other <code>Thread</code> methods.
 * This is important because classes should not be subclassed
 * unless the programmer intends on modifying or enhancing the fundamental
 * behavior of the class.
 *
 * @author  Arthur van Hoff
 * @see     java.lang.Thread
 * @see     java.util.concurrent.Callable
 * @since   JDK1.0
 */
@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

Runable使用函数表达式

        //不使用函数表达式
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.print("hello biaoge");
            }
        }).start();
        //使用函数表达式
        new Thread(()->System.out.print("hello biaoge")).start();

从上面来看,函数表达式明显方便简洁

猜你喜欢

转载自blog.csdn.net/jeekmary/article/details/81484864