【Lamdba表达式介绍一】

参考文章

1 为什么使用Lamdba表达式
https://dzone.com/articles/why-we-need-Lambda-expressions
2 什么是函数式编程
http://eloquentjavascript.net/1st_edition/chapter6.html
3 lambda文章
https://testerhome.com/topics/3567

自我感受Lamdba

虽然有的时候会使用Lamdba表达式,但有的时候只是简单使用,并没有做深入的了解,当然看到一些比较复杂的表达式,还是会上网查,琢磨好久,所以想着把Lamdba从根学好,顺便总结一下,输出分享~

传递的是行为,而不是值,这是让我最兴奋,最能提现他价值的地方,让我体会了抽象,哈哈哈,我比较菜~

lambda给我的感受就是好用,让我体会抽象,哈哈,通过参数传递行为,这种感觉超棒~一个函数式接口可以搞定很多事情,不用在方法体内写具体执行方法,对于Java本身而言好用很多。

这种图我感觉容纳了很多知识点,以后的分享从这里的点滴分享~
在这里插入图片描述

什么是Lamdba表达式

Lambda表达式是函数式接口,对于小白来说听到函数式接口的时候,其实内心应该想的是什么是函数式接口?简单点说:就没事没有方法名,修饰符,返回值。其实跟js大同小异。

Lambda的书写形式是: (argument) -> (body) 。 把这种格式记住,基本大同小异。有两部分组成,参数+函数体

Lamdba表达式列举格式

()->{System.out.print("judy")}
(String  judy)->{System.out.print(judy)}
(int a1 , int a2)->{System.out.print(a+b)}

从它的格式中我们可以看出可以接受参数,也可以午餐,花括号内使用的是具体的执行行为。
有的时候我们可以省略花括号,类似于你写if判断的时候只有一条语句的时候可以忽略花括号

Lamdba表达式的演进过程

1 jdk5

     List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println("------java 5---");

2 jdk8

        for (Integer integer : list) {
            System.out.println(integer);
        }
        System.out.println("------java8----");

3 函数式接口

     List list1 = Arrays.asList(1, 2, 3, 4);
        list1.forEach(i->{
            System.out.println(i);
        });

4 方法引用

list1.forEach(System.out::println);

Lamdba为什么出现

对于Java而言他是面向对象的,但是Java缺少了函数式编程的概念,当Lamdba出现的时候正好弥补了java 这块的缺陷

无论是什么事物,一旦出现肯定有他的道理,我曾经有一个面试官问我为什么会有Lambda表达式,内心想的是当然是因为他高效和好用啊。把原理说出来就好了

借用我看其他博主的文章的例子,我感觉千言万语不如用代码来说,哈哈,使用lamdba的写法

public static void main(String[] args) {
    System.out.println(test.compore(1, item -> {
            return item * 2;
        }));

        System.out.println(test.compore(1, item -> {
            return 1 % item;
        }));
        System.out.println(test.compore(1, item -> {
            return 1 / item;
        }));
        System.out.println(test.compore(1, item -> {
            return 1 - item;
        }));
}
     private int compore(int a, Function<Integer, Integer> function) {
          //无论谁调用,传递的都是行为。
        int result = function.apply(a);
        return result;
    }

如果没有使用lamdba,那么你可能需要这样写。


    private int method1(int a) {
        return 1 * a;
    }

    private int method2(int b) {
        return 1 / b;
    }

    private int method3(int b) {
        return 1 % b;
    }

什么是函数式接口?

函数式接口必须满足的条件

1 一个接口只有一个抽象方法,
2 在接口上声明@FunctionInterface

举个栗子

Function不陌生吧,接口上定义的就是@FunctionalInterface

/**
 * Represents a function that accepts one argument and produces a result.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #apply(Object)}.
 *
 * @param <T> the type of the input to the function
 * @param <R> the type of the result of the function
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);
    }

最后举个栗子

 public static void main(String[] args) {
        JFrame jFrame = new JFrame("My JFrame");
        JButton jButton = new JButton("My JButton");
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Button Judy");
            }
        });
 }

//虽然没有@FunctionalInterface 修饰,但是他只有一个抽象方法。
public interface ActionListener extends EventListener {

    /**
     * Invoked when an action occurs.
     */
    public void actionPerformed(ActionEvent e);

}


    /**
     * Adds an <code>ActionListener</code> to the button.
     * @param l the <code>ActionListener</code> to be added
     */
    public void addActionListener(ActionListener l) {
        listenerList.add(ActionListener.class, l);
    }

//如果用Lamdba我么怎么写?
jButton.addActionListener((ActionEvent event) -> System.out.println("Button Judy"));


总结

只是简单的做了一个前期的铺垫,之后大概有两篇见解lambda。原持续前进。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王雪芬-ghqr-264962

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

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

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

打赏作者

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

抵扣说明:

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

余额充值