【Java8新特性】還沒搞懂函數式接口?趕快過來看看吧!

寫在前面

Java8中內置了一些在開發中經常使用的函數式接口,極大的提升了咱們的開發效率。那麼,問題來了,你知道都有哪些函數式接口嗎?java

函數式接口總覽

這裏,我使用表格的形式來簡單說明下Java8中提供的函數式接口。微信

四大核心函數式接口

首先,咱們來看四大核心函數式接口,以下所示。app

函數式接口 參數類型 返回類型 使用場景
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還提供了一些其餘的函數式接口。dom

函數式接口 參數類型 返回類型 使用場景
BiFunction(T, U, R) T, U R 對類型爲T,U的參數應用操做,返回R類型的結果。接口定義的方法:R apply(T t, U u)
UnaryOperator (Function子接口) T T 對類型爲T的對象進行一 元運算, 並返回T類型的 結果。 包含方法爲 T apply(T t)
BinaryOperator (BiFunction 子接口) 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的定義以下所示。ide

@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接口的定義以下所示。ui

@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中四大核心函數式接口的用法,其餘函數式接口咱們也就知道如何使用了!this

寫在最後

若是以爲文章對你有點幫助,請微信搜索並關注「 冰河技術 」微信公衆號,跟冰河學習Java8新特性。code

最後,附上Java8新特性核心知識圖,祝你們在學習Java8新特性時少走彎路。

相關文章
相關標籤/搜索