Java8 函数式接口-Functional Interface

函数式接口:

函数式接口,首先是一个接口,然后就是在这个接口里面只能有一个抽象方法,但是可以有多个非抽象方法的接口。

Java 8为函数式接口引入了一个新注解@FunctionalInterface,主要用于编译级错误检查,加上该注解,当你写的接口不符合函数式接口定义的时候,编译器会报错。

函数式接口可以被隐式转换为 lambda 表达式。

Java 8的库帮你在java.util.function包中引入了几个新的函数式接口。我们接下来介绍 Predicate、Consumer和Function 三种函数式接口。

public interface Predicate<T> {
    boolean test(T t);
}

public interface Comparator<T> {
    int compare(T o1, T o2);
}

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}

JDK 8之前已有的函数式接口:

  • java.lang.Runnable
  • java.util.concurrent.Callable
  • java.security.PrivilegedAction
  • java.util.Comparator
  • java.io.FileFilter
  • java.nio.file.PathMatcher
  • java.lang.reflect.InvocationHandler
  • java.beans.PropertyChangeListener
  • java.awt.event.ActionListener
  • javax.swing.event.ChangeListener

新定义的函数式接口:

java.util.function中定义了几组类型的函数式接口以及针对基本数据类型的子接口。

  • Predicate -- 传入一个参数,返回一个bool结果, 方法为boolean test(T t)
  • Consumer -- 传入一个参数,无返回值,纯消费。 方法为void accept(T t)
  • Function -- 传入一个参数,返回一个结果,方法为R apply(T t)
  • Supplier -- 无参数传入,返回一个结果,方法为T get()
  • UnaryOperator -- 一元操作符, 继承Function,传入参数的类型和返回类型相同。
  • BinaryOperator -- 二元操作符, 传入的两个参数的类型和返回类型相同, 继承BiFunction

函数式接口中可以额外定义多个Object的public方法一样抽象方法:

接口最终有确定的类实现, 而类的最终父类是Object。 因此函数式接口可以定义Object的public方法。
如以下的接口依然是函数式接口:

@FunctionalInterface
public interface ObjectMethodFunctionalInterface {
	void count(int i);
	
	String toString(); //same to Object.toString
	int hashCode(); //same to Object.hashCode
	boolean equals(Object obj); //same to Object.equals
}

为什么限定public类型的方法呢?因为接口中定义的方法都是public类型的。 举个例子,下面的接口就不是函数式接口:

interface WrongObjectMethodFunctionalInterface {
	void count(int i);
	
	Object clone(); //Object.clone is protected
}

因为Object.clone方法是protected类型。

声明异常:

函数式接口的抽象方法可以声明 可检查异常(checked exception)。 在调用目标对象的这个方法时必须catch这个异常。

public class FunctionalInterfaceWithException {
	public static void main(String[] args) {
		InterfaceWithException target = i -> {};
		try {
			target.apply(10);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
@FunctionalInterface
interface InterfaceWithException {
	void apply(int i) throws Exception;
}

这和以前的接口/方法调用一样。

但是,如果在Lambda表达式中抛出异常, 而目标接口中的抽象函数没有声明这个可检查, 则此接口不能作为此lambda表达式的目标类型。

public class FunctionalInterfaceWithException {
	public static void main(String[] args) {
		InterfaceWithException target = i -> {throw new Exception();};
	}
}

@FunctionalInterface
interface InterfaceWithException {
	void apply(int i);
}

上面的例子中不能编译, 因为lambda表达式要求的目标类型和InterfaceWithException不同。 InterfaceWithException的函数没有声明异常。

静态方法:

函数式接口中除了那个抽象方法外还可以包含静态方法。
Java 8以前的规范中接口中不允许定义静态方法。 静态方法只能在类中定义。 Java 8中可以定义静态方法。

一个或者多个静态方法不会影响SAM接口成为函数式接口。
下面的例子中FunctionalInterfaceWithStaticMethod包含一个SAM: apply,还有一个静态方法sum。 它依然是函数式接口。

@FunctionalInterface
interface FunctionalInterfaceWithStaticMethod {
	static int sum(int[] array) {
		return Arrays.stream(array).reduce((a, b) -> a+b).getAsInt();
	}
	
	void apply();
}

public class StaticMethodFunctionalInterface {
	public static void main(String[] args) {
		int sum = FunctionalInterfaceWithStaticMethod.sum(new int[]{1,2,3,4,5});
		
		FunctionalInterfaceWithStaticMethod f = () -> {};
	}
}

默认方法

Java 8中允许接口实现方法, 而不是简单的声明, 这些方法叫做默认方法,使用特殊的关键字default
因为默认方法不是抽象方法,所以不影响我们判断一个接口是否是函数式接口。

@FunctionalInterface
interface InterfaceWithDefaultMethod {
	void apply(Object obj);
	
	default void say(String name) {
		System.out.println("hello " + name);
	}

}

class FunctionalInterfaceWithDefaultMethod {
	public static void main(String[] args) {
		InterfaceWithDefaultMethod i = (o) -> {};
		i.apply(null);
		i.say("default method");
	}
}

InterfaceWithDefaultMethod仍然是一个函数式接口。

泛型及继承关系

接口可以继承接口。 如果父接口是一个函数接口, 那么子接口也可能是一个函数式接口。 判断标准依据下面的条件:

对于接口I, 假定M是接口成员里的所有抽象方法的继承(包括继承于父接口的方法), 除去具有和Object的public的实例方法签名的方法, 那么我们可以依据下面的条件判断一个接口是否是函数式接口, 这样可以更精确的定义函数式接口。
如果存在一个一个方法m, 满足:

  • m的签名(subsignature)是M中每一个方法签名的子签名(signature)
  • m的返回值类型是M中的每一个方法的返回值类型的替代类型(return-type-substitutable)
    那么I就是一个函数式接口。

具体看参考中加粗的文章。

@FunctionalInterface:

Java 不会强制要求你使用@FunctionalInterface注解来标记你的接口是函数式接口, 然而,作为API作者, 你可能倾向使用@FunctionalInterface指明特定的接口为函数式接口, 这只是一个设计上的考虑, 可以让用户很明显的知道一个接口是函数式接口。

@FunctionalInterface
public interface SimpleFuncInterface {
    public void doWork();
}

如果你在一个不是函数式的接口使用@FunctionalInterface标记的话,会出现什么情况?编译时出错。

error: Unexpected @FunctionalInterface annotation
@FunctionalInterface
^
  I is not a functional interface
    multiple non-overriding abstract methods found in interface I

高阶函数:

Function:

@FunctionalInterface
public interface Function<T, R> {

    R apply(T t);

    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

其中实现了2个默认方法,分别compose,andThen,对应的函数表达为:

compose对应,体现嵌套关系;

andThen对应,转换了嵌套的顺序;

identity对应了一个传递自身的函数调用对应

从这里看出来,compose和andThen对于两个函数f和g来说,f.compose(g)等价于g.andThen(f)

public class TestFunction {
    public static void main(String[] args) {
        Function<Integer, Integer> incr1 = x -> x + 1;
        Function<Integer, Integer> multiply = x -> x * 2;
        int x = 2;
        System.out.println("f(x)=x+1,when x=" + x + ", f(x)=" + incr1.apply(x));
        System.out.println("f(x)=x+1,g(x)=2x, when x=" + x + ", f(g(x))=" + incr1.compose(multiply).apply(x));
        System.out.println("f(x)=x+1,g(x)=2x, when x=" + x + ", g(f(x))=" + incr1.andThen(multiply).apply(x));
        System.out.println("compose vs andThen:f(g(x))=" + incr1.compose(multiply).apply(x) + "," + multiply.andThen(incr1).apply(x));
    }
}

output:

f(x)=x+1,when x=2, f(x)=3
f(x)=x+1,g(x)=2x, when x=2, f(g(x))=5
f(x)=x+1,g(x)=2x, when x=2, g(f(x))=6
compose vs andThen:f(g(x))=5,5

拓展:

Function<Integer, Function<Integer, Integer>> makeAdder = z -> y -> z + y;

比如这个函数定义,参数是z,返回值是一个Function,这个Function本身又接受另一个参数y,返回z+y。于是我们可以根据这个函数,定义任意加法函数:

//high order function
Function<Integer, Function<Integer, Integer>> makeAdder = z -> y -> z + y;
x = 2;
//define add1
Function<Integer, Integer> add1 = makeAdder.apply(1);
System.out.println("f(x)=x+1,when x=" + x + ", f(x)=" + add1.apply(x));
//define add5
Function<Integer, Integer> add5 = makeAdder.apply(5);
System.out.println("f(x)=x+5,when x=" + x + ", f(x)=" + add5.apply(x));

由于高阶函数接受一个函数作为参数,结果返回另一个函数,所以是典型的函数到函数的映射。

BiFunction提供了二元函数的一个接口声明,举例来说:

//binary function
BiFunction<Integer, Integer, Integer> multiply = (a, b) -> a * b;
System.out.println("f(z)=x*y, when x=3,y=5, then f(z)=" + multiply.apply(3, 5));

其输出结果将是:f(z)=x*y, when x=3,y=5, then f(z)=15
二元函数没有compose能力,只是默认实现了andThen。

有了一元和二元函数,那么可以通过组合扩展出更多的函数可能。

Function接口相关的接口包括:

  • BiFunction :R apply(T t, U u);接受两个参数,返回一个值,代表一个二元函数;
  • DoubleFunction :R apply(double value);只处理double类型的一元函数;
  • IntFunction :R apply(int value);只处理int参数的一元函数;
  • LongFunction :R apply(long value);只处理long参数的一元函数;
  • ToDoubleFunction:double applyAsDouble(T value);返回double的一元函数;
  • ToDoubleBiFunction:double applyAsDouble(T t, U u);返回double的二元函数;
  • ToIntFunction:int applyAsInt(T value);返回int的一元函数;
  • ToIntBiFunction:int applyAsInt(T t, U u);返回int的二元函数;
  • ToLongFunction:long applyAsLong(T value);返回long的一元函数;
  • ToLongBiFunction:long applyAsLong(T t, U u);返回long的二元函数;
  • DoubleToIntFunction:int applyAsInt(double value);接受double返回int的一元函数;
  • DoubleToLongFunction:long applyAsLong(double value);接受double返回long的一元函数;
  • IntToDoubleFunction:double applyAsDouble(int value);接受int返回double的一元函数;
  • IntToLongFunction:long applyAsLong(int value);接受int返回long的一元函数;
  • LongToDoubleFunction:double applyAsDouble(long value);接受long返回double的一元函数;
  • LongToIntFunction:int applyAsInt(long value);接受long返回int的一元函数;

Operator:

Operator其实就是Function,函数有时候也叫作算子。算子在Java8中接口描述更像是函数的补充,和上面的很多类型映射型函数类似。

算子Operator包括:UnaryOperator和BinaryOperator。分别对应单元算子和二元算子。
单元算子的接口声明如下:

@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {
	static <T> UnaryOperator<T> identity() {
		return t -> t;
	}
}

二元算子的声明:

@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
    public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
    }

    public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
    }
}

很明显,算子就是一个针对同类型输入输出的一个映射。在此接口下,只需声明一个泛型参数T即可。对应上面的例子:

public class TestOperator {
    public static void main(String[] args) {
        UnaryOperator<Integer> add = x -> x + 1;
        System.out.println(add.apply(1));
        BinaryOperator<Integer> addxy = (x, y) -> x + y;
        System.out.println(addxy.apply(3, 5));
        BinaryOperator<Integer> min = BinaryOperator.minBy((o1, o2) -> o1 - o2);
        System.out.println(min.apply(100, 200));
        BinaryOperator<Integer> max = BinaryOperator.maxBy((o1, o2) -> o1 - o2);
        System.out.println(max.apply(100, 200));
    }
}

例子里补充一点的是,BinaryOperator提供了两个默认的static快捷实现,帮助实现二元函数min(x,y)和max(x,y),使用时注意的是排序器可别传反了:)

其他的Operator接口:(不解释了)

  • LongUnaryOperator:long applyAsLong(long operand);
  • IntUnaryOperator:int applyAsInt(int operand);
  • DoubleUnaryOperator:double applyAsDouble(double operand);
  • DoubleBinaryOperator:double applyAsDouble(double left, double right);
  • IntBinaryOperator:int applyAsInt(int left, int right);
  • LongBinaryOperator:long applyAsLong(long left, long right);

Predicate:

predicate是一个谓词函数,主要作为一个谓词演算推导真假值存在,其意义在于帮助开发一些返回bool值的Function。本质上也是一个单元函数接口,其抽象方法test接受一个泛型参数T,返回一个boolean值。等价于一个Function的boolean型返回值的子集。

@FunctionalInterface
public interface Predicate<T> {

    boolean test(T t);

    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

    default Predicate<T> negate() {
        return (t) -> !test(t);
    }

    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }
}

其默认方法也封装了and、or和negate逻辑。

写个小例子看看:

public class TestJ8Predicate {
    public static void main(String[] args) {
        TestJ8Predicate testJ8Predicate = new TestJ8Predicate();
        testJ8Predicate.printBigValue(10, val -> val > 5);
        testJ8Predicate.printBigValueAnd(10, val -> val > 5);
        testJ8Predicate.printBigValueAnd(6, val -> val > 5);
        //binary predicate
        BiPredicate<Integer, Long> biPredicate = (x, y) -> x > 9 && y < 100;
        System.out.println(biPredicate.test(100, 50L));
    }
    public void printBigValue(int value, Predicate<Integer> predicate) {
        if (predicate.test(value)) {
            System.out.println(value);
        }
    }
    public void printBigValueAnd(int value, Predicate<Integer> predicate) {
        if (predicate.and(v -> v < 8).test(value)) {
            System.out.println("value < 8 : " + value);
        } else {
            System.out.println("value should < 8 at least.");
        }
    }
} 

Output:

10
value should < 8 at least.
value < 8 : 6
true

Predicate在Stream中有应用,Stream的filter方法就是接受Predicate作为入参的。这个具体在后面使用Stream的时候再分析深入。

其他Predicate接口:

  • BiPredicate:boolean test(T t, U u);接受两个参数的二元谓词
  • DoublePredicate:boolean test(double value);入参为double的谓词函数
  • IntPredicate:boolean test(int value);入参为int的谓词函数
  • LongPredicate:boolean test(long value);入参为long的谓词函数

Consumer:

看名字就可以想到,这像谓词函数接口一样,也是一个Function接口的特殊表达——接受一个泛型参数,不需要返回值的函数接口。

@FunctionalInterface
public interface Consumer<T> {

    void accept(T t);

    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

这个接口声明太重要了,对于一些纯粹consume型的函数,没有Consumer的定义真无法被Function家族的函数接口表达。因为Function一定需要一个泛型参数作为返回值类型(当然不排除你使用Function来定义,但是一直返回一个无用的值)。比如下面的例子,如果没有Consumer,类似的行为使用Function表达就一定需要一个返回值。

public class TestJ8Consumer {
    public static void main(String[] args) {
        Consumer<Integer> consumer = System.out::println;
        consumer.accept(100);
        //use function, you always need one return value.
        Function<Integer, Integer> function = x -> {
            System.out.println(x);
            return x;
        };
        function.apply(100);
    }
}

其他Consumer接口:

  • BiConsumer:void accept(T t, U u);接受两个参数
  • DoubleConsumer:void accept(double value);接受一个double参数
  • IntConsumer:void accept(int value);接受一个int参数
  • LongConsumer:void accept(long value);接受一个long参数
  • ObjDoubleConsumer:void accept(T t, double value);接受一个泛型参数一个double参数
  • ObjIntConsumer:void accept(T t, int value);接受一个泛型参数一个int参数
  • ObjLongConsumer:void accept(T t, long value);接受一个泛型参数一个long参数

Supplier

最后说的是一个叫做Supplier的函数接口,其声明如下:

@FunctionalInterface
public interface Supplier<T> {
    T get();
}

其简洁的声明,会让人以为不是函数。这个抽象方法的声明,同Consumer相反,是一个只声明了返回值,不需要参数的函数(这还叫函数?)。也就是说Supplier其实表达的不是从一个参数空间到结果空间的映射能力,而是表达一种生成能力,因为我们常见的场景中不止是要consume(Consumer)或者是简单的map(Function),还包括了new这个动作。而Supplier就表达了这种能力。

比如你要是返回一个常量,那可以使用类似的做法:

 Supplier<Integer> supplier = () -> 1;
 System.out.println(supplier.get());

这保证supplier对象输出的一直是1。
如果是要利用构造函数的能力呢?就可以这样:

        Supplier<TestJ8Supplier> anotherSupplier;
        for (int i = 0; i < 10; i++) {
            anotherSupplier = TestJ8Supplier::new;
            System.out.println(anotherSupplier.get());
        }

这样的输出可以看到,全部的对象都是new出来的。

这样的场景在Stream计算中会经常用到,具体在分析Java 8中Stream的时候再深入。

其他Supplier接口:

  • BooleanSupplier:boolean getAsBoolean();返回boolean
  • DoubleSupplier:double getAsDouble();返回double
  • IntSupplier:int getAsInt();返回int
  • LongSupplier:long getAsLong();返回long

总结

整个函数式接口的大概总结如下:

名称 一元接口 说明 二元接口 说明
一般函数 Function 一元函数,抽象apply方法 BiFunction 二元函数,抽象apply方法
算子函数(输入输出同类型) UnaryOperator 一元算子,抽象apply方法 BinaryOperator 二元算子,抽象apply方法
谓词函数(输出boolean) Predicate 一元谓词,抽象test方法 BiPredicate 二元谓词,抽象test方法
消费者(无返回值) Consumer 一元消费者函数,抽象accept方法 BiConsumer 二元消费者函数,抽象accept方法
供应者(无参数,只有返回值) Supplier 供应者函数,抽象get方法 - -

参考:

stream之第三步终止操作(终端操作)

Java8 Stream系列(一)从入坑到沉迷

Java8特性① Lambda 表达式

Java 8函数式接口functional interface的秘密

JAVA 8 函数式接口 - Functional Interface

一起爪哇Java 8(一)——函数式接口

Java 函数式接口

Java8学习笔记(1) -- 从函数式接口说起

原文地址:https://www.cnblogs.com/hongdada/p/10021816.html