【Java8新特性】Lambda表达式

Lambda表达式是什么

Lambda是Java1.8的新特性,是一个匿名函数,即没有函数名的函数。了解函数式接口是我们学习Lambda的关键所在

如代码所示,就是一个函数式接口

interface Iflowers{
    abstract void one();
}

函数式接口: 即只有一个抽象方法的接口

为什么要使用Lambda表达式

因为我们可以通过Lambda表达式创建函数式接口对象,简化复杂的代码

传统实现函数式接口的方法:
public class LambdaDemo {
    public static void main(String[] args) {
        Iflowers flowers = new Flowers();
        flowers.one();
    }
}
//接口
interface Iflowers{
    abstract void one();
}

//实现类
class Flowers implements  Iflowers{
    public void one(){
        System.out.println("玫瑰花");
    }
}

输出:

玫瑰花
简化一:静态内部类:
public class LambdaDemo {
    //静态内部类
    static class Flowers implements  Iflowers{
        public void one(){
            System.out.println("玫瑰花");
        }
    }

    public static void main(String[] args) {
        Iflowers flowers = new Flowers();
        flowers.one();
    }
}
//函数式接口
interface Iflowers{
    abstract void one();
}

输出:

玫瑰花
简化二:局部内部类
public class LambdaDemo {
    public static void main(String[] args) {
    	//局部内部类
        class Flowers implements  Iflowers{
            public void one(){
                System.out.println("玫瑰花");
            }
        }

        Iflowers flowers = new Flowers();
        flowers.one();
    }
}
//函数式接口
interface Iflowers{
    abstract void one();
}

输出:

玫瑰花
简化三:匿名内部类
public class LambdaDemo {
    public static void main(String[] args) {
        //匿名内部类:没有名字的类
        Iflowers flowers = new Iflowers() {
            public void one() {
                System.out.println("玫瑰花");
            }
        };
        flowers.one();
    }
}
//函数式接口
interface Iflowers{
    abstract void one();
}

输出:

玫瑰花
简化四:Lambda表达式
public class LambdaDemo {
    public static void main(String[] args) {
        //Lambda表达式
        Iflowers flowers =  ()-> {
            System.out.println("玫瑰花");
        };
        flowers.one();
    }
}
//函数式接口
interface Iflowers{
    abstract void one();
}

输出:

玫瑰花

由以上可以看出,从最基本的调用方式—静态内部类—局部内部类—匿名内部类—函数式接口,我们的调用方式越来越简单,最开始还需要一个实现类,到Lambda表达式的时候我们只需要一个表达式即可完成对象的调用。

那么我们是否可以对Lambda再进行简化呢?

简化Lambda表达式

刚刚我们写的Lambda表达式是这样子的,现在我们给函数式接口加上参数

public class LambdaDemo {
    public static void main(String[] args) {
        //Lambda表达式
        Iflowers flowers = (String i,String j)-> {
            System.out.println(i+"--->"+j);
        };
        flowers.one("玫瑰花","我爱你");
    }
}
//函数式接口
interface Iflowers{
    abstract void one(String name, String meaning);
}
简化一:参数类型
public class LambdaDemo {
    public static void main(String[] args) {
        //Lambda表达式
        Iflowers flowers = (i,j)-> {
            System.out.println(i+"--->"+j);
        };
        flowers.one("玫瑰花","我爱你");
    }
}
//函数式接口
interface Iflowers{
    abstract void one(String name, String meaning);
}
简化二:去掉括号(有条件)
  • 参数只有一个才能去掉参数的括号,如果有多个参数则不能去掉
  • 执行体的内容只有一行的时候才能去掉执行体的括号,否则不可以去掉(执行体:System.out.println(i);)
public class LambdaDemo {
    public static void main(String[] args) {
        //Lambda表达式
        Iflowers flowers = i-> System.out.println(i);
        flowers.one("玫瑰花");
    }
}
//函数式接口
interface Iflowers{
    abstract void one(String name);
}
Lambda表达式的必要条件
  • 接口必须是函数式接口
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值