概述
對於只有一個抽象方法的接口,需要這種接口的對象,就可以提供一個lambda表達式。這種接口稱爲函數式接口(functional interface)。
在定義函數式接口的時候可以加入@FunctionalInterface
註解來修飾該接口
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface {}
JDK1.8常用函數式接口
函數式接口 | 參數類型 | 返回類型 | 抽象方法名 | 描述 | 其他方法 |
---|---|---|---|---|---|
Rnnnable |
無 | void | run | 作爲無參數或者返回值的動作運行 | |
Supplier<T> |
無 | T | get | 提供一個T類型的值 | |
Consumer<T> |
T | void | accept | 處理一個T類型的數據 | andThen |
BiConsumer<T, U> |
T ,U | void | accept | 處理T類型和U類型的數據 | andThen |
Function<T, R> |
T | R | apply | 有一個T類型的參數,返回R類型的值 | compse, andThen, identity |
BiFunction<T,U,R> |
T,U | R | apply | 一個T類型參數,和U類型的參數,返回R類型值 | andThen |
Perdicate<T> |
T | boolean | test | 布爾值函數 | and,or,negate,isEqual |
Supplier<T>
接口
函數抽象方法無參數,返回一個T類型的值
/**
* @author justLym
* @version 1.0.0 2020/3/11 19:54
**/
public class SupplierDemo {
public static void main(String[] args) {
test(() -> "justlym");
}
public static void test(Supplier<String> supplier) {
System.out.println(supplier.get());
}
}
Consumer<T>
接口
/**
* @author justLym
* @version 1.0.0 2020/3/11 19:57
**/
public class ConsumerDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("justLym");
list.forEach((item) -> System.out.println(item));
test("justLym", s -> {s += "1";}, s -> {s += "2";});
}
public static void test(String string, Consumer<String> consumer , Consumer<String> consumer2) {
consumer.andThen(consumer2).accept(string);
}
}
Perdicate<T>
接口
抽象方法返回布爾值,用於方法執行過程中判斷使用
public class PredicateDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("justLym");
list.add("1234");
list.removeIf((item) -> item.startsWith("justLym"));
list.forEach(System.out::print);
}
}
Predicate<T>
接口其他方法
Predicate<T> and(Predicate<? super T> other)
返回一個Predicate對象,兩個函數式接口組合結果取結果的與,相當於邏輯判斷的&&
Predicate<T> negate()
返回一個Predicate對象該函數接口的test方法執行結果取反,相當於邏輯判斷的!
Predicate<T> or(Predicate<? super T> other)
返回一個Predicate對象,兩個函數式接口組合結果取或,相當去邏輯判斷的||
static <T> Predicate<T> isEqual(Object targetRef)
返回一個Predicate對象, 這是一個靜態方法,相當於Objects.equals(Object, Object).
/**
* @author justLym
* @version 1.0.0 2020/3/11 20:09
**/
public class PredicateDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("justLym");
list.add("1234");
Predicate<String> justLym = Predicate.isEqual("justLym");
list.removeIf(justLym);
list.forEach(System.out::print);
}
}
Function<T,R>
接口
/**
* @author justLym
* @version 1.0.0 2020/3/11 20:34
**/
public class FunctionDemo {
public static void main(String[] args) {
Integer test = test("1", Integer::parseInt);
System.out.println(test);
}
/**
* 將String類型轉成Integer類型
* @param string
* @param function
* @return
*/
public static Integer test(String string, Function<String, Integer> function) {
return function.apply(string);
}
/**
* 將String類型轉成Integer類型,再將Integer轉成String類型
* @param string
* @param function
* @param function1
* @return
*/
public static String test(String string, Function<String, Integer> function, Function<Integer, String> function1) {
return function.andThen(function1).apply(string);
}
}
public static void main(String[] args) {
Function<Integer, Integer> times2 = i -> i*2;
Function<Integer, Integer> squared = i -> i*i;
System.out.println(times2.apply(4));
System.out.println(squared.apply(4));
//32 先4×4然後16×2,先執行apply(4),在times2的apply(16),先執行參數,再執行調用者。
System.out.println(times2.compose(squared).apply(4));
//64 先4×2,然後8×8,先執行times2的函數,在執行squared的函數。
System.out.println(times2.andThen(squared).apply(4));
//16
System.out.println(Function.identity().compose(squared).apply(4));
}