Home >Java >javaTutorial >New features of Java 8: Lambda expressions

New features of Java 8: Lambda expressions

黄舟
黄舟Original
2017-02-23 10:34:571468browse

Abstract: Lambda expression is one of several important new features brought to us by Java 8. Borrowing lambda expression can make our Java program design more concise. This article is the first of the new features of Java 8 and will explore behavior parameterization, lambda expressions, and method references.

Lambda expression is one of several important new features brought to us by java8. Borrowing lambda expression can make our java program design more concise. Recently, new projects have abandoned version 1.6 and are fully developed based on Java8. This article is the first article on the new features of Java8 and will discuss behavior parameterization, lambda expressions, and method references.

1. Behavior Parameterization

Behavior parameterization simply means that the main body of the function only contains the general code of the template class, and some will change with the business scenario. Logic is passed to the function in the form of parameters. Using behavioral parameterization can make the program more versatile to cope with frequent changes.

Consider a business scenario. Suppose we need to filter apples through a program. We first define an apple entity:

/**
 * 苹果实体
 *
 * @author zhenchao.wang 2016-09-17 12:49
 * @version 1.0.0
 */
public class Apple {
    /** 编号 */
    private long id;
    /** 颜色 */
    private Color color;
    /** 重量 */
    private float weight;
    /** 产地 */
    private String origin;
    public Apple() {
    }
    public Apple(long id, Color color, float weight, String origin) {
        this.id = id;
        this.color = color;
        this.weight = weight;
        this.origin = origin;
    }
    // 省略getter和setter
}

The user’s initial needs may be simple We hope that we can filter out green apples through the program, so we can quickly implement it through the program:

/**
 * 筛选绿苹果
 *
 * @param apples
 * @return
 */
public static List<Apple> filterGreenApples(List<Apple> apples) {
    List<Apple> filterApples = new ArrayList<>();
    for (final Apple apple : apples) {
        if (Color.GREEN.equals(apple.getColor())) {
            filterApples.add(apple);
        }
    }
    return filterApples;
}

If after a while the user puts forward new requirements, we hope that we can use the program to filter out green apples. Filter out red apples, so we specifically added the function of filtering out red apples:

/**
 * 筛选红苹果
 *
 * @param apples
 * @return
 */
public static List<Apple> filterRedApples(List<Apple> apples) {
    List<Apple> filterApples = new ArrayList<>();
    for (final Apple apple : apples) {
        if (Color.RED.equals(apple.getColor())) {
            filterApples.add(apple);
        }
    }
    return filterApples;
}

A better implementation is to pass the color as a parameter to the function, so that It can handle various color filtering requests from users in the future:

/**
 * 自定义筛选颜色
 *
 * @param apples
 * @param color
 * @return
 */
public static List<Apple> filterApplesByColor(List<Apple> apples, Color color) {
    List<Apple> filterApples = new ArrayList<>();
    for (final Apple apple : apples) {
        if (color.equals(apple.getColor())) {
            filterApples.add(apple);
        }
    }
    return filterApples;
}

After designing it like this, you no longer have to worry about changes in users’ color filtering needs. But unfortunately, one day The user asked for a requirement to be able to select apples whose weight reaches a certain standard. With the previous lesson, we also passed the weight standard as a parameter to the screening function, so we got:

/**
 * 筛选指定颜色,且重要符合要求
 *
 * @param apples
 * @param color
 * @param weight
 * @return
 */
public static List<Apple> filterApplesByColorAndWeight(List<Apple> apples, Color color, float weight) {
    List<Apple> filterApples = new ArrayList<>();
    for (final Apple apple : apples) {
        if (color.equals(apple.getColor()) && apple.getWeight() >= weight) {
            filterApples.add(apple);
        }
    }
    return filterApples;
}

Is this method of passing parameters really good? If there are more and more filtering conditions and the combination mode becomes more and more complex, do we need to consider all situations and have corresponding strategies for each situation? And these functions are only different in part of the filtering conditions. , the rest is the same template code (traversing the collection), at this time we can parameterize the behavior , so that the function only retains the template code, and extracts the filtering conditions and passes them in as parameters. Before java8, we implemented this by defining a filter interface:

/**
 * 苹果过滤接口
 *
 * @author zhenchao.wang 2016-09-17 14:21
 * @version 1.0.0
 */
@FunctionalInterface
public interface AppleFilter {
    /**
     * 筛选条件抽象
     *
     * @param apple
     * @return
     */
    boolean accept(Apple apple);
}
/**
 * 将筛选条件封装成接口
 *
 * @param apples
 * @param filter
 * @return
 */
public static List<Apple> filterApplesByAppleFilter(List<Apple> apples, AppleFilter filter) {
    List<Apple> filterApples = new ArrayList<>();
    for (final Apple apple : apples) {
        if (filter.accept(apple)) {
            filterApples.add(apple);
        }
    }
    return filterApples;
}

After abstracting the above behavior, we can set the filtering conditions at the specific call place and pass the conditions as parameters In the method:

public static void main(String[] args) {
    List<Apple> apples = new ArrayList<>();
    // 筛选苹果
    List<Apple> filterApples = filterApplesByAppleFilter(apples, new AppleFilter() {
        @Override
        public boolean accept(Apple apple) {
            // 筛选重量大于100g的红苹果
            return Color.RED.equals(apple.getColor()) && apple.getWeight() > 100;
        }
    });
}

The above behavior parameterization method is implemented using anonymous classes. This design is also often used within jdk, such as

java.util.Comparator

,

java.util.concurrent.Callable

, etc. When using this type of interface, we can use anonymous classes to specify the specific execution logic of the function at the specific call place. However, judging from the above code block, although it is extremely Customer, but not concise enough, in java8 we can simplify it through lambda:

// 筛选苹果
List<Apple> filterApples = filterApplesByAppleFilter(apples,
        (Apple apple) -> Color.RED.equals(apple.getColor()) && apple.getWeight() >= 100);

The code is greatly simplified through lambda expressions. Let’s learn java lambda expressions~

2. Lambda expression definition

We can define lambda expression as a concise and transitive anonymous function, first we It needs to be clear that a lambda expression is essentially a function. Although it does not belong to a specific class, it has a parameter list, a function body, a return type, and the ability to throw exceptions. Secondly, it is anonymous, and lambda expressions have no specific Function name; lambda expression can be passed like a parameter, thus greatly simplifying code writing. The format is defined as follows:

Format 1: Parameter list-> Expression
Format 2: Parameter list-> {expression set}
It should be noted that lambda expressions imply return keyword, so in a single expression, we do not need to explicitly write the return keyword, but when the expression is a collection of statements, you need to explicitly add return and use curly braces

{ }

Surround multiple expressions, here are a few examples:

//返回给定字符串的长度,隐含return语句
(String s) -> s.length() 
// 始终返回42的无参方法
() -> 42 
// 包含多行表达式,则用花括号括起来
(int x, int y) -> {
    int z = x * y;
    return x + z;
}

3. Relying on functional interfaces to use lambda expressions

Requirements for using lambda expressions With the help of functional interfaces, that is to say, only where functional interfaces appear, we can simplify them with lambda expressions.

Custom functional interface

A functional interface is defined as an interface with only one abstract method. The improvement in interface definition in Java 8 is the introduction of default methods, which allows us to provide default implementations for methods in the interface. However, no matter how many default methods there are, as long as there is one and only one abstract method, then it is a functional interface. As follows (quoting the AppleFilter above):

/**
 * 苹果过滤接口
 *
 * @author zhenchao.wang 2016-09-17 14:21
 * @version 1.0.0
 */
@FunctionalInterface
public interface AppleFilter {
    /**
     * 筛选条件抽象
     *
     * @param apple
     * @return
     */
    boolean accept(Apple apple);
}
AppleFilter

contains only one abstract method

accept(Apple apple)

. According to the definition, it can be regarded as a functional interface. When defining, we Added

to this interface
@FunctionalInterface

注解,用于标记该接口是函数式接口,不过这个接口是可选的,当添加了该接口之后,编译器就限制了该接口只允许有一个抽象方法,否则报错,所以推荐为函数式接口添加该注解。

jdk自带的函数式接口

jdk为lambda表达式已经内置了丰富的函数式接口,如下表所示(仅列出部分):




函数式接口 函数描述符 原始类型特化
Predicate8742468051c85b06f0a0af9e3e506b5c T -> boolean IntPredicate, LongPredicate, DoublePredicate
Consumer8742468051c85b06f0a0af9e3e506b5c T -> void IntConsumer, LongConsumer, DoubleConsumer
Funcation469df1b70b2914a3841e0404ec3e3d5d T -> R IntFuncation0f1763a9dfcc95d54eac98034f0cdcdd, IntToDoubleFunction, IntToLongFunction0f1763a9dfcc95d54eac98034f0cdcdd, LongFuncation…
Supplier8742468051c85b06f0a0af9e3e506b5c () -> T BooleanSupplier, IntSupplier, LongSupplier, DoubleSupplier
UnaryOperator8742468051c85b06f0a0af9e3e506b5c T -> T IntUnaryOperator, LongUnaryOperator, DoubleUnaryOperator
BinaryOperator8742468051c85b06f0a0af9e3e506b5c (T, T) -> T IntBinaryOperator, LongBinaryOperator, DoubleBinaryOperator
BiPredicateedf640cc243cef8451ed278a13e0f190 (L, R) -> boolean
BiConsumerabbd655bd3f9f929be0207abcc18a2ef (T, U) -> void
BiFunction96b8c9ba1c8b330d622a8468a6113c2e (T, U) -> R

下面分别就

Predicate<T>

Consumer<T>

Function<T, R>

的使用示例说明。

Predicate8742468051c85b06f0a0af9e3e506b5c

@FunctionalInterface
public interface Predicate<T> {
    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
}

Predicate的功能类似于上面的

AppleFilter

,利用我们在外部设定的条件对于传入的参数进行校验,并返回验证结果

boolean

,下面利用

Predicate

对List集合的元素进行过滤:

/**
 * 按照指定的条件对集合元素进行过滤
 *
 * @param list
 * @param predicate
 * @param 
 * @return
 */
public  List filter(List list, Predicate<T> predicate) {
    List newList = new ArrayList();
    for (final T t : list) {
        if (predicate.test(t)) {
            newList.add(t);
        }
    }
    return newList;
}

利用上面的函数式接口过滤字符串集合中的空字符串:

demo.filter(list, (String str) -> null != str && !str.isEmpty());
Consumer<T>
@FunctionalInterface
public interface Consumer<T> {
    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);
}

Consumer提供了一个accept抽象函数,该函数接收参数,但不返回值,下面利用

Consumer

遍历集合:

/**
 * 遍历集合,执行自定义行为
 *
 * @param list
 * @param consumer
 * @param 
 */
public  void filter(List list, Consumer<T> consumer) {
    for (final T t : list) {
        consumer.accept(t);
    }
}

利用上面的函数式接口,遍历字符串集合,并打印非空字符串:

demo.filter(list, (String str) -> {
        if (StringUtils.isNotBlank(str)) {
            System.out.println(str);
        }
    });

Function469df1b70b2914a3841e0404ec3e3d5d

@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);
}

Funcation执行转换操作,输入是类型T的数据,返回R类型的数据,下面利用

Function

对集合进行转换:

/**
 * 遍历集合,执行自定义转换操作
 *
 * @param list
 * @param function
 * @param 
 * @param 
 * @return
 */
public  List filter(List list, Function<T, R> function) {
    List newList = new ArrayList();
    for (final T t : list) {
        newList.add(function.apply(t));
    }
    return newList;
}

下面利用上面的函数式接口,将一个封装字符串(整型数字的字符串表示)的接口,转换成整型集合:

demo.filter(list, (String str) -> Integer.parseInt(str));
上面这些函数式接口还提供了一些逻辑操作的默认实现,留到后面介绍java8接口的默认方法时再讲吧~

使用过程中需要注意的一些事情

类型推断

在编码过程中,有时候可能会疑惑我们的调用代码会去具体匹配哪个函数式接口,实际上编译器会根据参数、返回类型、异常类型(如果存在)等做正确的判定。

在具体调用时,在一些时候可以省略参数的类型,从而进一步简化代码:

/

/ 筛选苹果
List<Apple> filterApples = filterApplesByAppleFilter(apples,
        (Apple apple) -> Color.RED.equals(apple.getColor()) && apple.getWeight() >= 100);
// 某些情况下我们甚至可以省略参数类型,编译器会根据上下文正确判断
List<Apple> filterApples = filterApplesByAppleFilter(apples,
        apple -> Color.RED.equals(apple.getColor()) && apple.getWeight() >= 100);

局部变量

上面所有例子我们的lambda表达式都是使用其主体参数,我们也可以在lambda中使用局部变量,如下:

int weight = 100;
List<Apple> filterApples = filterApplesByAppleFilter(apples,
        apple -> Color.RED.equals(apple.getColor()) && apple.getWeight() >= weight);

该例子中我们在lambda中使用了局部变量weight,不过在lambda中使用局部变量必须要求该变量 显式声明为final或事实上的final ,这主要是因为局部变量存储在栈上,lambda表达式则在另一个线程中运行,当该线程视图访问该局部变量的时候,该变量存在被更改或回收的可能性,所以用final修饰之后就不会存在线程安全的问题。

四. 方法引用

采用方法引用可以更近一步的简化代码,有时候这种简化让代码看上去更加的直观,先看一个例子:

/* ... 省略apples的初始化操作 */
// 采用lambda表达式
apples.sort((Apple a, Apple b) -> Float.compare(a.getWeight(), b.getWeight()));
// 采用方法引用
apples.sort(Comparator.comparing(Apple::getWeight));

方法引用通过

::

将方法隶属和方法自身连接起来,主要分为三类:

静态方法

(args) -> ClassName.staticMethod(args)


转换成

ClassName::staticMethod


参数的实例方法

(args) -> args.instanceMethod()


转换成

ClassName::instanceMethod  // ClassName是args的类型


外部的实例方法

(args) -> ext.instanceMethod(args)


转换成

ext::instanceMethod(args)

 以上就是Java8 新特性之 Lambda 表达式 的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn