寫在前面
Java8中內(nèi)置了一些在開發(fā)中常用的函數(shù)式接口,極大的提高了我們的開發(fā)效率。那么,問題來了,你知道都有哪些函數(shù)式接口嗎?
函數(shù)式接口總覽
這里,我使用表格的形式來簡單說明下Java8中提供的函數(shù)式接口。
四大核心函數(shù)式接口
首先,我們來看四大核心函數(shù)式接口,如下所示。
函數(shù)式接口 | 參數(shù)類型 | 返回類型 | 使用場景 |
Consumer |
T | void | 對類型為T的對象應(yīng)用操作,接口定義的方法:void accept(T t) |
Supplier |
無 | T | 返回類型為T的對象,接口定義的方法:T get() |
Function<T, R>函數(shù)式接口 | T | R | 對類型為T的對象應(yīng)用操作,并R類型的返回結(jié)果。接口定義的方法:R apply(T t) |
Predicate |
T | boolean | 確定類型為T的對象是否滿足約束條件,并返回boolean類型的數(shù)據(jù)。接口定義的方法:boolean test(T t) |
其他函數(shù)接口
除了四大核心函數(shù)接口外,Java8還提供了一些其他的函數(shù)式接口。
函數(shù)式接口 | 參數(shù)類型 | 返回類型 | 使用場景 |
BiFunction(T, U, R) | T, U | R | 對類型為T,U的參數(shù)應(yīng)用操作,返回R類型的結(jié)果。接口定義的方法:R apply(T t, U u) |
UnaryOperator |
T | T | 對類型為T的對象進(jìn)行一 元運(yùn)算, 并返回T類型的 結(jié)果。 包含方法為 T apply(T t) |
BinaryOperator |
T, T | T | 對類型為T的對象進(jìn)行二 元運(yùn)算, 并返回T類型的 結(jié)果。 包含方法為 T apply(T t1, T t2) |
BiConsumer<T, U> | T, U | void | 對類型為T, U 參數(shù)應(yīng)用 操作。 包含方法為 void accept(T t, U u) |
ToIntFunction |
T | int | 計(jì)算int值的函數(shù) |
ToLongFunction |
T | long | 計(jì)算long值的函數(shù) |
ToDoubleFunction |
T | double | 計(jì)算double值的函數(shù) |
IntFunction |
int | R | 參數(shù)為int 類型的函數(shù) |
LongFunction |
long | R | 參數(shù)為 long類型的函數(shù) |
DoubleFunction |
double | R | 參數(shù)為double類型的函數(shù) |
四大核心函數(shù)式接口
Consumer接口
1.接口說明
Consumer接口是消費(fèi)性接口,無返回值。Java8中對Consumer的定義如下所示。
1
2
3
4
5
6
7
8
9
10
|
@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); }; } } |
2.使用示例
1
2
3
4
5
6
7
8
|
public void handlerConsumer(Integer number, Consumer<Integer> consumer){ consumer.accept(number); } @Test public void test1(){ this .handlerConsumer( 10000 , (i) -> System.out.println(i)); } |
Supplier接口
1.接口說明
Supplier接口是供給型接口,有返回值,Java8中對Supplier接口的定義如下所示。
1
2
3
4
|
@FunctionalInterface public interface Supplier<T> { T get(); } |
2.使用示例
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public List<Integer> getNumberList( int num, Supplier<Integer> supplier){ List<Integer> list = new ArrayList<>(); for ( int i = 0 ; i < num; i++){ list.add(supplier.get()) } return list; } @Test public void test2(){ List<Integer> numberList = this .getNumberList( 10 , () -> new Random().nextInt( 100 )); numberList.stream().forEach(System.out::println); } |
Function接口
1.接口說明
Function接口是函數(shù)型接口,有返回值,Java8中對Function接口的定義如下所示。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@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.使用示例
1
2
3
4
5
6
7
8
9
|
public String handlerString(String str, Function<String, String> func){ return func.apply(str); } @Test public void test3(){ String str = this .handlerString( "binghe" , (s) -> s.toUpperCase()); System.out.println(str); } |
Predicate接口
1.接口說明
Predicate接口是斷言型接口,返回值類型為boolean,Java8中對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
|
@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); } } |
2.使用示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public List<String> filterString(List<String> list, Predicate<String> predicate){ List<String> strList = new ArrayList<>(); for (String str : list){ if (predicate.test(str)){ strList.add(str); } } return strList; } @Test public void test4(){ List<String> list = Arrays.asList( "Hello" , "Lambda" , "binghe" , "lyz" , "World" ); List<String> strList = this .filterString(list, (s) -> s.length() >= 5 ); strList.stream().forEach(System.out::println); } |
注意:只要我們學(xué)會了Java8中四大核心函數(shù)式接口的用法,其他函數(shù)式接口我們也就知道如何使用了!
寫在最后
最后,附上Java8新特性核心知識圖,祝大家在學(xué)習(xí)Java8新特性時(shí)少走彎路。
以上就是詳解JAVA8 函數(shù)式接口的詳細(xì)內(nèi)容,更多關(guān)于JAVA8 函數(shù)式接口的資料請關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://www.cnblogs.com/binghe001/p/12846649.html