函数式接口
特点:有且仅有一个抽象方法的接口
检测是否为函数式接口:@FunctionalInterface,放在接口定义的上方,如果接口是函数式接口,编译通过,不是则编译失败
@FunctionalInterface是可选的,只要保证满足函数式接口定义的条件,也是函数式接口
1 2 3 4
| @FunctionalInterface public interface MyInter { void show(); }
|
- 如果方法的参数是一个函数式接口,可以使用Lambda表达式作为参数传递
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static void main(String[] args) { startThread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"启动"); } }); startThread(()-> System.out.println(Thread.currentThread().getName()+"启动")); }
private static void startThread(Runnable r){ new Thread(r).start(); }
|
- 如果方法的返回值是一个函数式接口,可以使用Lambda表达式作为结果返回
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
| public static void main(String[] args) { ArrayList<String>arrayList=new ArrayList<String>(); arrayList.add("das"); arrayList.add("fdfs"); arrayList.add("vcxvv"); Collections.sort(arrayList,getComparator()); System.out.println(arrayList); }
private static Comparator<String> getComparator(){
return (s1,s2)-> s1.length()-s2.length(); }
|
Comparator(比较器)
1 2 3 4
| @FunctionalInterface public interface Comparator<T> { int compare(T o1, T o2); }
|
1 2 3 4 5
| Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName); Person p1 = new Person("John", "Doe"); Person p2 = new Person("Alice", "Wonderland"); comparator.compare(p1, p2); comparator.reversed().compare(p1, p2);
|
Supplier(供应)
1 2 3 4 5
| @FunctionalInterface public interface Supplier<T> { T get(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void main(String[] args) { String s = getString(() -> "da"); System.out.println(s); Integer integer = getInteger(() -> 123); System.out.println(integer); }
private static String getString(Supplier<String> supplier) { return supplier.get(); }
private static Integer getInteger(Supplier<Integer> supplier) { return supplier.get(); }
|
Consumer(消费)
1 2 3 4 5 6 7 8 9 10 11
| @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 static void main(String[] args) { operatorString("da", s-> System.out.println(s)); operatorString("da", System.out::println); System.out.println("-----------------------"); operatorString("da", s-> System.out.println(s), s-> System.out.println(new StringBuilder(s).reverse().toString())); }
private static void operatorString(String s, Consumer<String>consumer) { consumer.accept(s); }
private static void operatorString(String s, Consumer<String>consumer, Consumer<String>consumer1) { consumer.andThen(consumer1).accept(s); }
|
Predicate(断言)
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
| @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 static void main(String[] args) { boolean cxz = checkString("cxz", s -> s.length() > 8); System.out.println(cxz); boolean ccxz = checkString("ccxz", s -> s.length() > 3, s -> s.length() < 11); System.out.println(ccxz); }
private static boolean checkString(String s, Predicate<String>predicate){ return predicate.negate().test(s); }
private static boolean checkString(String s, Predicate<String> predicate, Predicate<String> predicate1){
return predicate.or(predicate1).test(s); }
|
Function(功能)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @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
| public static void main(String[] args) { convert("100", s-> Integer.parseInt(s)); convert("100", Integer::parseInt); convert(100, i-> String.valueOf(i+100)); convert("100", s-> Integer.parseInt(s), s-> String.valueOf(s+100)); }
private static void convert(String s, Function<String,Integer> function) { Integer apply = function.apply(s); System.out.println(apply); }
private static void convert(int i, Function<Integer,String> function) { String apply = function.apply(i); System.out.println(apply); }
private static void convert(String s, Function<String,Integer> function, Function<Integer,String> function1) { String apply = function.andThen(function1).apply(s); System.out.println(apply); }
|