Hello World

Lambda 表达式

1. Lambda表达式 

 Lambda表达式是jdk1.8里面的一个重要的更新,这意味着java也开始承认了函数式编程,并且尝试引入其中。

  函数式编程的一个特点就是,允许把函数本身作为参数传入另一个函数,还允许返回一个函数!

2. 函数式接口

  “函数式接口”是指仅仅只包含一个抽象方法的接口,每一个该类型的lambda表达式都会被匹配到这个抽象方法。

  JDK1.8提供了一个@FunctionalInterface注解来定义函数式接口,如果我们定义的接口不符合 FunctionalInterface 的规范便会报错。

3. @FunctionalInterface  

/*
 * 该注解用于表明这个接口是一个功能接口
 * 由Java语言规范定义
 *
 * An informative annotation type used to indicate that an interface
 * type declaration is intended to be a <i>functional interface</i> as
 * defined by the Java Language Specification.
 *
 * 从概念上讲,一个功能接口只有一个准确的抽象方法
 *
 * Conceptually, a functional interface has exactly one abstract method. 
 *
 * 如果一个接口声明一个 java.lang.Object 的方法,则该方法不计入接口的抽象方法计数
 * 因为任何接口的实现都会有来自 java.lang.Object 或其他地方的实现。
 *
 * If an interface declares an abstract method overriding one of the
 * public methods of {@code java.lang.Object}, that also does
 * <em>not</em> count toward the interface's abstract method count
 * since any implementation of the interface will have an
 * implementation from {@code java.lang.Object} or elsewhere.
 */

4. 代码示例

package com.skd.lambda;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class LmabdaTest {
    public static String[] params = new String[]{"aa", "bb", "cc"};
    public static void main(String[] args) {
        start();
        compare();
        testLamda();
        testLamdaWithParams();

    }

    /**
     * 创建线程
     */
    public static void start() {
        final int i = 0;
        /**
         * 常规写法
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("常规写法:i=" + i);
            }
        }).start();

        // 方法没有参数的示例,参数对应的()为空

        /**
         *  不带参数类型的Lambda表达式
         *  ()-> 方法体
         */
        new Thread(() -> System.out.println("不带参数类型的Lambda表达式:i=" + i)).start();

        /**
         *  带参数类型的Lambda表达式
         *  (类名信息)()->{
         *      方法体
         *  }
         */
        new Thread((Runnable) ()->{
            System.out.println("带参数类型的Lambda表达式:i=" + i);
        }).start();

    }

    // 方法有参数的示例

    /**
     * 常规的Collections的排序的写法,需要对接口方法重写
     */
    public static void compare() {
        List<String> list = Arrays.asList(params);
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });
        for (String string : list) {
            System.out.println(string);
        }
    }

    /**
     *  不带参数类型的Lambda表达式
     *   如果方法有返回值,自动将结果返回
     *  (参数...)-> 方法体
     */
    public static void testLamda() {
        List<String> list = Arrays.asList(params);
        Collections.sort( list, (a, b) -> b.compareTo(a) );
        for (String string : list) {
            System.out.println(string);
        }
    }

    /**
     *   带参数类型的Lambda表达式
     *   如果方法有返回值,必须显示将结果返回
     *  (类名信息)(参数类型 参数...)->{
     *      方法体
     *  }
     */
    public static void testLamdaWithParams() {
        List<String> list = Arrays.asList(params);
        Collections.sort(list, (Comparator<? super String>) (String a, String b) -> {
                    return b.compareTo(a);
                }
        );
        for (String string : list) {
            System.out.println(string);
        }
    }
}

 

posted @ 2018-12-27 11:26  小小忧愁米粒大  阅读(182)  评论(0编辑  收藏  举报
瞅啥瞅,好好看书