009、lambda表达式(三更草堂)

一、函数式编程

1、概念

主要关注的是对数据进行了什么操作,类似数学中的函数。

2、优点

  • 代码简洁、开发迅速
  • 接近自然语言、易于理解
  • 易于“并发编程”

二、Lambda表达式

1、概述

对某些匿名内部类的写法进行简化

2、基本格式

(参数列表)->{代码}

3、例子

  • 线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("常规写法.....");
            }
        }).start();
        new Thread(()->{
            System.out.println("lambda表达式写法.....");
        }).start();
  • IntBinaryOperator
 public class Demo02 {
    public static void main(String[] args) {
    	// 匿名内部类
        int calculateNum = calculateNum(new IntBinaryOperator() { 
            @Override
            public int applyAsInt(int left, int right) {
                return left + right;
            }
        });
        // lambda表达式
        int calculateNum1 = calculateNum((left, right) -> {
            return left + right;
        });
        System.out.println(calculateNum);
        System.out.println(calculateNum1);
    }

    public static int calculateNum(IntBinaryOperator operator) {
        int a = 10, b = 20;
        return operator.applyAsInt(a, b);
    }
}
  • 快捷键:
    • alt+enter,可以将lambda表达式转成匿名内部类,也可以将匿名内部类简化成lambda表达式
  • IntPredicate
public class Demo03 {
    public static void main(String[] args) {
        // 匿名内部类
        printNum(new IntPredicate() {
            @Override
            public boolean test(int value) {
                return value % 2 == 0;
            }
        });
        // lambda表达式
        printNum((value -> {
            return value % 2 != 0;
        }));
    }

    public static void printNum(IntPredicate predicate) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int i : arr) {
            if (predicate.test(i)) {
                System.out.println(i);
            }
        }
    }
}
  • Function<T, R>
public class Demo04 {
    public static void main(String[] args) {
        // 匿名内部类
        System.out.println(typeConvert("123", new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return Integer.parseInt(s);
            }
        }));
        // lambda表达式
        System.out.println(typeConvert("123", (String s) -> {
            return s + "hi";
        }));
    }

    // 将String类型的转换成R类型
    public static <R> R typeConvert(String str, Function<String, R> function) {
        return function.apply(str);
    }
}
  • IntConsumer
public class Demo05 {
    public static void main(String[] args) {
        // 匿名内部类
        forEachArr(new int[]{1, 2, 3, 4, 5, 6}, new IntConsumer() {
            @Override
            public void accept(int value) {
                System.out.println(value);
            }
        });
        // lambda表达式
        forEachArr(new int[]{1, 2, 3, 4, 5, 6}, s -> {
            System.out.println(s);
        });
    }

    public static void forEachArr(int[] arr, IntConsumer consumer) {
        for (int i : arr) {
            consumer.accept(i);
        }
    }
}

4、省略规则

  • 参数类型可以省略
  • 方法体只有一句代码时,可以省略return和;
  • 方法只有一个参数时,()可以省略
  • alt+enter,快捷键

5、最终简洁版本

public class Demo06 {
    public static void main(String[] args) {
        new Thread(() -> System.out.println("lambda表达式,实现runnable接口...")).start();

        System.out.println(calculateNum(((left, right) -> left + right)));

        printNum(num -> num % 2 == 0);

        System.out.println(typeConvert("123", (String s) -> s + "hi~~~"));

        forEachArr(new int[]{1, 2, 3}, num -> System.out.println(num));


    }

    public static int calculateNum(IntBinaryOperator operator) {
        int a = 10, b = 20;
        return operator.applyAsInt(a, b);
    }

    public static void printNum(IntPredicate predicate) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int i : arr) {
            if (predicate.test(i)) {
                System.out.println(i);
            }
        }
    }

    public static <R> R typeConvert(String str, Function<String, R> function) {
        return function.apply(str);
    }

    public static void forEachArr(int[] arr, IntConsumer consumer) {
        for (int i : arr) {
            consumer.accept(i);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值