
上一章已经说了函数式接口的基本概念(java8系列-01 Lambdas 表达式)。函数式接口(Functional Interface)说白了就是只包含了一个抽象方法的接口。下面来剖析一下最常用的四类函数式接口:
++提示:本章更多的用代码来叙述,不会更多的文字。++
Function功能型函数式接口
上一章有讲到,Function接口是接收一个T类型的参数,返回一个R类型的结果。
Function接口源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @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; } }
|
代码例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public class FunctionTest { static String modifyU(String str, Function<String,String> funs){ return funs.apply(str); }
static String modifyU2(String str, Function<String,String> funs){ return funs.andThen(funs).apply(str); }
static String modifyU3(String str, Function<String,String> funs){ return funs.compose(funs).compose(funs).apply(str); }
public static void main(String[] args) { User u = new User(); String header = modifyU("hello ", (x)->x.concat(u.getName()==null?"Stranger":u.getName())); System.out.println(header);
String header2 = modifyU("hello ", new Function<String, String>() { @Override public String apply(String s) { return s.concat(u.getName()==null?"Stranger":u.getName()); } }); System.out.println(header2);
String header3 = modifyU2("hello ",(x)->x.concat(u.getName()==null?"Stranger ":u.getName())); System.out.println(header3);
String header4 = modifyU3("hello ",(x)->x.concat(u.getName()==null?"Stranger ":u.getName())); System.out.println(header4); } }
|
Consumer消费型函数式接口
Consumer接口接收一个T类型的参数,不返回结果
接口源码
1 2 3 4 5 6 7 8 9 10 11 12
| @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); }; } }
|
代码例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class ConsumerTest { static void modifyU4(String str, Consumer<String> cons){ cons.accept(str); }
public static void main(String[] args) { modifyU4("Hello ",(x)-> System.out.println(x+"java8")); modifyU4("Hello ", new Consumer<String>() { @Override public void accept(String s) { System.out.println(s+"java8"); } }); } }
|
Predicate断言型函数式接口
Predicate接收一个T类型的参数,返回一个boolen类型的结果
接口源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| @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); } }
|
代码例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class PredicateTest {
static boolean modifyU5(User u, Predicate<User> pre){ return pre.test(u); }
static Predicate<User> modifyU6(User u, Predicate<User> pre){ return pre.negate(); }
public static void main(String[] args) { User user = new User(); System.out.println(modifyU5(user, (e)-> user.getName() != null));
System.out.println(modifyU5(user,modifyU6(user,(e)-> user.getName() != null))); } }
|
Supplier供给型函数式接口
不接受参数,返回一个T类型的结果
接口源码
1 2 3 4 5 6 7 8 9 10
| @FunctionalInterface public interface Supplier<T> {
T get(); }
|
代码例子
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class SupplierTest {
static String modifyU7(Supplier<String> supplier){ return supplier.get(); }
public static void main(String[] args) { String str = "HelloJava8"; System.out.println(modifyU7(()-> String.valueOf(str.length()))); } }
|