寫在前面
Java8中內置了一些在開發中常用的函數式接口,極大的提高了我們的開發效率。那么,問題來了,你知道都有哪些函數式接口嗎?
函數式接口總覽
這里,我使用表格的形式來簡單說明下Java8中提供的函數式接口。
四大核心函數式接口
首先,我們來看四大核心函數式接口,如下所示。
函數式接口 | 參數類型 | 返回類型 | 使用場景 |
---|---|---|---|
Consumer
|
T | void | 對類型為T的對象應用操作,接口定義的方法:void accept(T t) |
Supplier
|
無 | T | 返回類型為T的對象,接口定義的方法:T get() |
Function<T, R>函數式接口 | T | R | 對類型為T的對象應用操作,並R類型的返回結果。接口定義的方法:R apply(T t) |
Predicate
|
T | boolean | 確定類型為T的對象是否滿足約束條件,並返回boolean類型的數據。接口定義的方法:boolean test(T t) |
其他函數接口
除了四大核心函數接口外,Java8還提供了一些其他的函數式接口。
函數式接口 | 參數類型 | 返回類型 | 使用場景 |
---|---|---|---|
BiFunction(T, U, R) | T, U | R | 對類型為T,U的參數應用操作,返回R類型的結果。接口定義的方法:R apply(T t, U u) |
UnaryOperator
|
T | T | 對類型為T的對象進行一 元運算, 並返回T類型的 結果。 包含方法為 T apply(T t) |
BinaryOperator
|
T, T | T | 對類型為T的對象進行二 元運算, 並返回T類型的 結果。 包含方法為 T apply(T t1, T t2) |
BiConsumer<T, U> | T, U | void | 對類型為T, U 參數應用 操作。 包含方法為 void accept(T t, U u) |
ToIntFunction
|
T | int | 計算int值的函數 |
ToLongFunction
|
T | long | 計算long值的函數 |
ToDoubleFunction
|
T | double | 計算double值的函數 |
IntFunction
|
int | R | 參數為int 類型的函數 |
LongFunction
|
long | R | 參數為 long類型的函數 |
DoubleFunction
|
double | R | 參數為double類型的函數 |
四大核心函數式接口
Consumer接口
1.接口說明
Consumer接口是消費性接口,無返回值。Java8中對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); };
}
}
2.使用示例
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接口的定義如下所示。
@FunctionalInterface
public interface Supplier<T> {
T get();
}
2.使用示例
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接口是函數型接口,有返回值,Java8中對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;
}
}
2.使用示例
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接口的定義如下所示。
@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.使用示例
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);
}
注意:只要我們學會了Java8中四大核心函數式接口的用法,其他函數式接口我們也就知道如何使用了!
寫在最后
如果覺得文章對你有點幫助,請微信搜索並關注「 冰河技術 」微信公眾號,跟冰河學習Java8新特性。
最后,附上Java8新特性核心知識圖,祝大家在學習Java8新特性時少走彎路。