您的位置:首页 > 编程语言 > Java开发

java 8之函数式接口

2018-01-28 12:55 253 查看
Java 8中函数式接口是一大新特性。本文就来说说Java 8 中的几种函数式接口。

一、Function接口

Function接口就是将传入的参数,通过一个或多个方法的迭代,输出和参数类型一样或者不一样的结果。

示例:

// Function<T, R>
//toInteger的Function中的T类型必须与toString的Function中的T类型相同,
Function<String, Integer> toInteger = s -> {
System.out.println("******");
return Integer.valueOf(s);
};
Function<String, Integer> toString = toInteger.andThen(Integer::valueOf);
//toObj的Function中的R类型必须与toString的Function中的R类型相同,compose方法中的参数类型必须与toString的Function中的T类型相同
Function<String, Integer> toObj = toString.compose(String::valueOf);
Integer a = toInteger.apply("3");
Integer b = toString.apply("123");
System.out.println(a);
System.out.println(b);
System.out.println(toObj.apply("123"));


Function接口源码:

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

/**
* 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 <V> 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 <V> 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 <T> 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<T, R> apply(t) = R
,然后再执行
Function<R, V> apply(R) = V
。 忽略过程,传入的参数为T,输出的结果为V。

我的理解为:假设n个andThen()迭代,那么就是不断用输出结果继续作为输入进行操作,最后一个出口的结果作为最终结果。

从源码上可以看出,先执行
Function<V, T> apply(v) = T
,然后再执行
Function<T, R> apply(T) = R
。忽略过程,初始传入的参数为T,输出的结果为R。

我的理解为:假设n个compose()迭代,那么就是为了得到R,前面的apply()不断输出,直至得到T。

static <T> Function<T, T> identity()
:返回一个输入和输出都为T的Function对象

二、Predicate接口

Predicate接口是起判断的作用,

示例:

Predicate<String> predicate = s -> s.length() > 0;
Predicate<String> predicate1 = s -> s.length() > 1;
boolean flag = predicate1.and(predicate).test("ss");
boolean flag1 = predicate.negate().test("ss");
System.out.println(flag);
System.out.println(flag1);


源码分析:

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

/**
* Returns a composed predicate that represents a short-circuiting logical
* AND of this predicate and another.  When evaluating the composed
* predicate, if this predicate is {@code false}, then the {@code other}
* predicate is not evaluated.
*
* <p>Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ANDed with this
*              predicate
* @return a composed predicate that represents the short-circuiting logical
* AND of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}

/**
* Returns a predicate that represents the logical negation of this
* predicate.
*
* @return a predicate that represents the logical negation of this
* predicate
*/
default Predicate<T> negate() {
return (t) -> !test(t);
}

/**
* Returns a composed predicate that represents a short-circuiting logical
* OR of this predicate and another.  When evaluating the composed
* predicate, if this predicate is {@code true}, then the {@code other}
* predicate is not evaluated.
*
* <p>Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ORed with this
*              predicate
* @return a composed predicate that represents the short-circuiting logical
* OR of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}

/**
* Returns a predicate that tests if two arguments are equal according
* to {@link Objects#equals(Object, Object)}.
*
* @param <T> the type of arguments to the predicate
* @param targetRef the object reference with which to compare for equality,
*               which may be {@code null}
* @return a predicate that tests if two arguments are equal according
* to {@link Objects#equals(Object, Object)}
*/
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}


test()方法相当于判断输入参数是否满足我们所约定的条件,返回true或false。

and()方法是要求同时满足多个条件。

or()方法是要求满足其中一个条件。

negate()方法是对test()方法的结果取反。

三、Supplier接口

Supplier接口主要用来返回一个任意范型的值,和Function接口不同的是该接口没有任何参数。

示例:

Supplier<Car> redCar = Car::new;
System.out.println(redCar.get());


四、Consumer接口

Consumer接口表示一个接受单个输入参数并且没有返回值的操作,可能会更改输入参数的内部状态

示例:

Car car = new Car();
Consumer<Car> blueCar = s -> s.setColor("blue");
blueCar.accept(car);
System.out.println(car.getColor());


返回结果:blue
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息