JAVA8 UnaryOperator接口

我们先看下这个接口的定义
@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {

    /**
     * Returns a unary operator that always returns its input argument.
     *
     * @param <T> the type of the input and output of the operator
     * @return a unary operator that always returns its input argument
     */
    static <T> UnaryOperator<T> identity() {
        return t -> t;
    }
}

这个接口继承Function接口,Funtion接口,定义了一个apply的抽象类,接收一个泛型T对象,并且返回泛型R对象

关于Funtcion的意思以及用法,可以移步这边Function接口的详细

这个接口,只接收一个泛型参数T,集成Function接口,也就是说,传入泛型T类型的参数,调用apply后,返回也T类型的参数;这个接口定义了一个静态方法,返回泛型对象的本身;

具体用法,可以参照Function接口详细

UnaryOperator<Integer> dda = x -> x + 1;
		System.out.println(dda.apply(10));// 11
		UnaryOperator<String> ddb = x -> x + 1;
		System.out.println(ddb.apply("aa"));// 11

猜你喜欢

转载自blog.csdn.net/qq_28410283/article/details/80634319
今日推荐