Java8新特性(二)—————Lambda语法

关注微信公众号【行走在代码行的寻路人】获取Java学习视频及资料。
  一、Lambda 表达式的基础语法:Java8中引入了一个新的操作符 "->" 该操作符称为箭头操作符或 Lambda 操作符
                              箭头操作符将 Lambda 表达式拆分成两部分:
 
  左侧:Lambda 表达式的参数列表
  右侧:Lambda 表达式中所需执行的功能, 即 Lambda 体
 
  语法格式一:无参数,无返回值
          () -> System.out.println("Hello Lambda!");
 
  语法格式二:有一个参数,并且无返回值
          (x) -> System.out.println(x)
 
  语法格式三:若只有一个参数,小括号可以省略不写
          x -> System.out.println(x)
 
  语法格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
         Comparator<Integer> com = (x, y) -> {
             System.out.println("函数式接口");
             return Integer.compare(x, y);
         };
 
  语法格式五:若 Lambda 体中只有一条语句, return 和 大括号都可以省略不写
          Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
 
  语法格式六:Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断”
          (Integer x, Integer y) -> Integer.compare(x, y);
 
 
  二、Lambda 表达式需要“函数式接口”的支持
  函数式接口:接口中只有一个抽象方法的接口,称为函数式接口。 可以使用注解 @FunctionalInterface 修饰
               可以检查是否是函数式接口

案列:

package com.company.test;

import org.junit.Test;

import java.util.*;
import java.util.function.Consumer;

/*
 * 一、Lambda 表达式的基础语法:Java8中引入了一个新的操作符 "->" 该操作符称为箭头操作符或 Lambda 操作符
 * 						    箭头操作符将 Lambda 表达式拆分成两部分:
 *
 * 左侧:Lambda 表达式的参数列表
 * 右侧:Lambda 表达式中所需执行的功能, 即 Lambda 体
 *
 * 语法格式一:无参数,无返回值
 * 		() -> System.out.println("Hello Lambda!");
 *
 * 语法格式二:有一个参数,并且无返回值
 * 		(x) -> System.out.println(x)
 *
 * 语法格式三:若只有一个参数,小括号可以省略不写
 * 		x -> System.out.println(x)
 *
 * 语法格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
 *		Comparator<Integer> com = (x, y) -> {
 *			System.out.println("函数式接口");
 *			return Integer.compare(x, y);
 *		};
 *
 * 语法格式五:若 Lambda 体中只有一条语句, return 和 大括号都可以省略不写
 * 		Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
 *
 * 语法格式六:Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断”
 * 		(Integer x, Integer y) -> Integer.compare(x, y);
 *
 *
 * 二、Lambda 表达式需要“函数式接口”的支持
 * 函数式接口:接口中只有一个抽象方法的接口,称为函数式接口。 可以使用注解 @FunctionalInterface 修饰
 * 			 可以检查是否是函数式接口
 */
public class MainTest02 {

    @Test
    public void test1(){
        int num = 0;//jdk 1.7 前,必须是 final

        Runnable r = new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello World!" + num);
            }
        };

        r.run();

        System.out.println("-------------------------------");

        Runnable r1 = () -> System.out.println("Hello Lambda!");
        r1.run();
    }

    @Test
    public void test2(){
        Consumer<String> con = x -> System.out.println(x);
        con.accept("Hello World!");
    }

    @Test
    public void test3(){
        Comparator<Integer> com = (x, y) -> {
            System.out.println("函数式接口");
            return Integer.compare(x, y);
        };
    }

    @Test
    public void test4(){
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
    }


    //需求:对一个数进行运算
    @Test
    public void test6(){
        Integer num = operation(100, (x) -> x * x);
        System.out.println(num);

        System.out.println(operation(200, (y) -> y + 200));
    }

    public Integer operation(Integer num, MyFun mf){
        return mf.getValue(num);
    }

}

需要学习资料和了解程序猿技术每日杂志的可以关注下微信公众【行走在代码行的寻路人】后续持续更新每天学习心得跟方法,跟大家一起成长!
 

发布了101 篇原创文章 · 获赞 10 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/WMY1230/article/details/102767472