java8新特性lambda表达式案例

/*
 * 一、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 修饰
 * 			 可以检查是否是函数式接口
 */
/*
     * @Author lyp
     * @Description 语法格式一:无参数,无返回值 () -> System.out.println("Hello Lambda!");
     * @Date 2021/5/3 11:02 
     * @Param 
     * @return 
     **/
    
    @Test
    public void test1(){

        //通过匿名内部类创建对象
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello World!");
            }
        };
        runnable.run();
        System.out.println("-------------------------------");
        //使用lambda表达式替换匿名内部类
        Runnable runnable2=()-> System.out.println("hello lambda");
        runnable2.run();
    }

 /*
     * @Author lyp
     * @Description 语法格式二:有一个参数,并且无返回值
     * 		(x) -> System.out.println(x)
     * @Date 2021/5/3 11:04
     * @Param
     * @return
     **/
    @Test
    public void test2(){
        Consumer<String> con = x -> System.out.println(x);
        con.accept("lyp-itjiaochen.club!");
    }
/*
     * @Author lyp
     * @Description 语法格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
     *		Comparator<Integer> com = (x, y) -> {
     *			System.out.println("函数式接口");
     *			return Integer.compare(x, y);
     *		};
     * @Date 2021/5/3 11:10
     * @Param
     * @return
     **/
    @Test
    public void test4(){
        Comparator<Integer> comparator=(x,y)->{
            System.out.println("函数式接口");
            return Integer.compare(x,y);
        };

        System.out.println(comparator.compare(56,56));
    }
package com.atguigu.java8;

@FunctionalInterface
public interface MyFun {

	public Integer getValue(Integer num);
	
}
//需求:对一个数进行运算
	@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);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT教程资源

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值