**
函数式接口
**
jdk1.8 函数式接口
- java.lang.Runnable
new Thread(()->System.out.println("开启一个线程!")).start();
- java.util.concurrent.Callable
ExecutorService threadPool = Executors.newSingleThreadExecutor();
Future<String> future = threadPool.submit(()->"创建了一个线程");
- java.util.Comparator
Collections.sort(list, (Comparator<Integer>) (o1, o2) -> o1.compareTo(o2));
jdk1.8 内置的函数式接口放在包 java.util.function 下,默认在jdk安装路径下的 src.zip 中.
这些接口,主要分4大类:
-
Consumer(类似于消费者需要传入参数无返回值)
-
Supplier(类似于生产者不需要传入参数,但有返回值)
-
Function(有输入也有返回)
-
Predicate(判断函数,有输入也有返回,返回true or false)
他们特点如下:
接口 参数类型 返回类型 方法 用途
-
Consumer T void void accept(T t) 对类型T参数操作,无返回结果
-
Supplier 无 T T get() 创造T类型参数
-
Function T R R apply(T t) 对类型T参数操作,返回R类型参数
-
Predicate T boolean boolean test(T t) 断言型接口,对类型T进行条件筛选操作
Consumer 接口源码:
@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); };
}
}
Consumer 表示接受单个输入参数并且不返回结果的操作:
public class Test {
private void test(double money, Consumer<Double> con){
con.accept(money);
}
@org.junit.Test
public void test1(){
double money = 10000;
test(money,m->System.out.println("这次消费了" + m + "元!"));
System.out.println(money);
}
}
由此可见,Consumer 的作用仅仅只是对传入的参数进行操作,并不是真正的“消费掉了”,因为函数式编程推崇的是不可变对象,Lambda表达式不允许修改“值”.
但Consumer的真正意义并不是简单的实现一个无返回值的单参数方法(或者说行为),而是由它的default方法所带来的嵌套调用(连锁调用),这种调用是无限次的.
public class Test {
private void test(double money, Consumer<Double> con){
con.accept(money);
}
@org.junit.Test
public void test1(){
double money = 10000;
Consumer consumer = m->System.out.println("这次消费了"+m+"元");
Consumer consumer1 = s->System.out.println("这次消费了"+s+"元");
test(money,consumer.andThen(consumer1.andThen(consumer)));
System.out.println(money);
}
}
执行结果如下:
Supplier 接口源码:
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Supplier 没有要求每次调用供应商时都会返回新的或不同的结果.
但这是两种编程思想的问题,Supplier提供的是一种行为,将行为规范化,为的是如果其他地方需要产生一个对象(比如100以内的整型,这当然可以直接传入对象,但如果是一个复杂对象,这个对象的创建过程特别复杂),Supplier可以为其提供一种产生该对象的规范行为并直接可作为参数传入.
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;
}
}
Function ,顾名思义,接受一个参数,经过处理后,再返回一个结果,其中 andThen() 方法接受一个行为,并将父方法处理过的结果作为参数再处理,而 compose() 方法正好相反,它是先自己处理然后将结果作为参数传给父方法执行,虽然处理的都是数据,但传入的却是行为.
@Test
public void test3(){
Function function = str->str+"suffix";
Function function1 = str->str+"prefix";
System.out.println(function.apply("111"));
System.out.println(function.andThen(function1).apply("111"));
System.out.println(function.compose(function1).apply("111"));
}
Predicate 接口源码:
@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);
}
简单来说,Predicate 也是一种行为的描述,对参入的对象作某些判断,并返回一个boolean值,这些判断逻辑即这个行为的具体实现过程,则完全由使用者定义,其中还提供一些简单的组合判断,与、或、非以及 isEqual.