Java Function中的容易被忽略的方法identity()

/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package java.util.function;

import java.util.Objects;

/**
 * Represents a function that accepts one argument and produces a result.
 * 表示接受一个参数并产生一个结果的函数。
 *       
 * @param  the type of the input to the function
 * 参数是函数的输入类型
 * @param  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 函数参数
     * @return R 函数返回类型
     */
    R apply(T t);

    /**
     * Returns a composed function that first applies the {@code before}
     * function to its input, and then applies this function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *
     * @param  the type of input to the {@code before} function, and to the
     *           composed function
     * @param before the function to apply before this function is applied
     * @return a composed function that first applies the {@code before}
     * function and then applies this function
     * @throws NullPointerException if before is null
     *
     * @see #andThen(Function)
     */
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    /**
     * Returns a composed function that first applies this function to
     * its input, and then applies the {@code after} function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     * 返回一个组合函数,该函数首先将该函数应用于其输入,然后将该函数应用于结果。如果对任一函数的求值抛出异常,则将其中继到组合函数的调用方。
     * @param  the type of output of the {@code after} function, and of the
     *           composed function
     * @param after the function to apply after this function is applied
     * @return a composed function that first applies this function and then
     * applies the {@code after} function
     * @throws NullPointerException if after is null
     *
     * @see #compose(Function)
     */
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    /**
     * Returns a function that always returns its input argument.
     * 该方法返回一个函数,该函数返回输入的参数。
     * @param  the type of the input and output objects to the function
     * @return a function that always returns its input argument
     */
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

之前只了解Function是函数式接口,支持Lambda表达式,今日由于需要,才了解到Functionidentity()

Functionidentity()返回t -> T,即本身。
也就是说identity()可以换为t -> T

今天的需求大概是:

已经有了List,利用stream()获取一个键值对Map

// 构造Map键值对,key:Integer, value:IndexEntity
// key为指标实体的id,value为对应的指标实体
Map<Integer, IndexEntity> map = indexEntities.stream().collect(Collectors.toMap(IndexEntity::getId, Function.identity()));

IndexEntity::getId是Java8新出的方法引用。

详细可以了解这篇文章:

《Java8新特性》上之Lambda表达式、函数式接口、方法引用、Optional

你可能感兴趣的:(Java基础)