• Java的Lambda表达式学习笔记:使用lambda表达式


    跟着官网的教程一起深入学习Lambda表达式

    Using Lambdas Expressions in Your Application - Dev.java

    二、运用Lambda表达式到程序中

    JDK8加了一个新包java.util.function,里面定义了很多函数式接口,主要分为4类:Supplier

    Consumer、Predicate、Function

    1.想获取一个新对象使用Supplier

    Supplier没有入参,会返回一个对象,而且每次调用都会返回一个新的对象

    1. @FunctionalInterface
    2. public interface Supplier {
    3. T get();
    4. }

    下面是一个简单的Supplier实现如下:

    1. public static void main(String[] args) {
    2. Supplier str = () -> "hello Duke";
    3. System.out.println(str.get());
    4. }

     再看一个实现示例:

    1. public static void main(String[] args) {
    2. Random random = new Random(314L);
    3. Supplier supplier = () -> random.nextInt(10);
    4. for (int i = 0; i < 5; i++) {
    5. System.out.println(supplier.get());
    6. }
    7. }

    上面的操作会两次装箱和拆箱操作,一个是random.nextInt(10)返回int时会自动装箱为Integer;另一个是调用get后获得随机数后(这里我自己也不是很懂,有人懂的赐教一下),这样的自动装箱拆箱会影响性能。考虑到这个问题,JDK提供了优化的方法供使用。比如提供了专门的IntSupplier:

    1. @FunctionalInterface
    2. public interface IntSupplier {
    3. int getAsInt();
    4. }

    用IntSupplier修改上面的例子:

    1. Random random = new Random(314L);
    2. IntSupplier newRandom = () -> random.nextInt();
    3. for (int i = 0; i < 5; i++) {
    4. int nextRandom = newRandom.getAsInt();
    5. System.out.println("next random = " + nextRandom);
    6. }

    JDK提供了四个这样的专用Supplier:IntSupplierBooleanSupplierLongSupplierDoubleSupplier.其中的get方法分别为getAsInt(),getAsBoolean(),getAsLong(),getAsDouble()

    2.需要使用一个对象用Consumer

     Consumer和Supplier相反,它需要入参,但不返回东西,而且它除了抽象方法还有一个default方法

    1. @FunctionalInterface
    2. public interface Consumer {
    3. void accept(T t);
    4. // default methods removed
    5. }

    在Supplier的例子中使用Consumer:

    1. public static void main(String[] args) {
    2. Random random = new Random(314L);
    3. IntSupplier supplier = () -> random.nextInt(10);
    4. Consumer consumer = s -> System.out.println(s);
    5. for (int i = 0; i < 5; i++) {
    6. consumer.accept(supplier.getAsInt());
    7. }
    8. }

    Consumer和Supplier一样,考虑到性能优化,提供了专门的IntConsumer,LongConsumer,DoubleConsumer,因为它们都没有返回值,方法均为accept()。

    JDK还提供了一个接收两个参数的BigConsumer

    1. @FunctionalInterface
    2. public interface BiConsumer {
    3. void accept(T t, U u);
    4. // default methods removed
    5. }

    使用BigConsumer重写写获取随机数的例子:

    1. public static void main(String[] args) {
    2. BiConsumer consumer = (r, n) -> {
    3. for (int j = 0; j < n; j++) {
    4. System.out.println(r.nextInt(10));
    5. }
    6. };
    7. consumer.accept(new Random(314L), 5);
    8. }

     BigConsumer也提供了专门的ObjIntConsumer,ObjLongConsumer,ObjDoubleConsumer

     3.当需要判断一个对象是否满足某个条件时用Predicate

    predicate常常会被用来做过滤

    1. @FunctionalInterface
    2. public interface Predicate {
    3. boolean test(T t);
    4. // default and static methods removed
    5. }

    Predicate的基本类型专用接口有IntPredicate,LongPredicate,DoublePredicate,同时也有支持两个入参的接口BigPredicate,但是BigPredicate没有基本类型的专用接口。

    用Predicate对list做过滤:

    1. public static void main(String[] args) {
    2. List list = List.of("one", "two", "three", "four", "five");
    3. List strings = new ArrayList<>(list);
    4. Predicate predicate = s -> s.length()%2==0;
    5. strings.removeIf(predicate);
    6. System.out.println(strings);
    7. }

    输出如下:

    注意这里的removeIf()会修改调用列表的内容,所以像List.of()和Arrays.asList()生成的list,调用removeIf()时会运行保存,因为这两种方式生成的列表都是不可变的。

    4.从一个类型映射到另一个类型用Function

    1. @FunctionalInterface
    2. public interface Function {
    3. R apply(T t);
    4. // default and static methods removed
    5. }

     例如:

    1. Function toLength = s -> s.length();
    2. String word = ...; // any kind of word will do
    3. int length = toLength.apply(word);

  • 相关阅读:
    世界前沿技术发展报告2023《世界航空技术发展报告》(四)无人机技术
    网络面试题总结
    不可不知的USB2.0/USB3.0/HDMI静电防护方案
    docker registry 镜像同步
    Calculation View里的Keep Flag 和 Transparent Filter
    sqlserver时间字段索引失效,重建后索引又正常了
    自定义vue组件发布npm仓库
    [原创]一种自动化九点标定工具原理(包涵部分源码)
    前端html生成PDF
    dgpnpsev启动项无效
  • 原文地址:https://blog.csdn.net/u011998957/article/details/126662818