Java8 新特性 四大內(nèi)置核心函數(shù)式接口及其擴(kuò)展

四大內(nèi)置核心函數(shù)式接口

Java內(nèi)置的函數(shù)式接口是為了方便開(kāi)發(fā)者使用Lambda表達(dá)式,對(duì)于應(yīng)對(duì)大部分函數(shù)式接口的使用而提出的。有了這些內(nèi)置接口,程序員不用自己在代碼中去定義,就能完成大部分工作需求。

  • Consumer<T>:消費(fèi)型接口(無(wú)返回值,有一個(gè)泛型的輸入?yún)?shù))
@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); };
   }
}
  • Supplier<T>:供給型接口(無(wú)輸入?yún)?shù),有一個(gè)泛型的返回值)
@FunctionalInterface
public interface Supplier<T> {
    T get();
}
  • Function<T,R>:函數(shù)型接口(有一個(gè)泛型的輸入?yún)?shù)和一個(gè)泛型的返回值)
@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));
    }
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}
  • Predicate<T>:斷言型(判斷型)接口:(有一個(gè)泛型的輸入?yún)?shù),返回一個(gè)bool值)
@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);
    }
}

從上面四個(gè)接口的源碼中可以看到,接口中不一定只有一個(gè)方法的才是函數(shù)式接口,一個(gè)接口是函數(shù)接口的充要條件是有且僅有一個(gè)抽象方法。接口中的其他方法可以通過(guò)加上default關(guān)鍵字提供默認(rèn)實(shí)現(xiàn),或者是一個(gè)static修飾的靜態(tài)方法。

四大內(nèi)置核心函數(shù)式接口使用方法。

public class TestLambda2 {
    //Consumer<T>消費(fèi)型接口:(無(wú)返回值,有一個(gè)泛型的輸入?yún)?shù))
    @Test
    public void test1() {
        happy(10000, money -> System.out.println("發(fā)了" + money + "元工資,開(kāi)心啊"));
    }
    //處理一個(gè)數(shù)據(jù)
    public void happy(double money, Consumer<Double> consumer) {
        consumer.accept(money);
    }

    //供給型接口(無(wú)輸入?yún)?shù),有一個(gè)泛型的返回值)
    @Test
    public void test2(){
        getNumList(10, () -> (int) (Math.random() * 100)).forEach(System.out::println);
    }
    //獲取一個(gè)整數(shù)集合
    public List<Integer> getNumList(int num, Supplier<Integer> supplier) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(supplier.get());
        }
        return list;
    }

    //Function<T,R>函數(shù)型接口(有一個(gè)泛型的輸入?yún)?shù)和一個(gè)泛型的返回值)
    @Test
    public void test3(){
        System.out.println(strHandler("\t\n    " + "  你好?。。?!哈哈   \r\n\t", String::trim));
    }
    //處理字符串后返回
    public String strHandler(String str, Function<String, String> function) {
        return function.apply(str);
    }

    //Predict<T> 斷言型(判斷型)接口:(有一個(gè)泛型的輸入?yún)?shù),返回一個(gè)bool值)
    @Test
    public void test4(){
        List<String> list = Arrays.asList("Hello","what","is","your","name");
        filterStr(list, s -> s.length() > 3).forEach(System.out::println);
    }
    //將滿足條件的字符串,放入集合中
    public List<String> filterStr(List<String> list, Predicate<String> predicate) {
        List<String> strings = new ArrayList<>();
        list.forEach(s -> {
            if (predicate.test(s)) {
                strings.add(s);
            }
        });
        return strings;
    }
}

上述代碼中使用了方法引用。即需要提供一個(gè)函數(shù)式接口的地方,不僅可以使用Lambda表達(dá)式,還可以使用函數(shù)的引用,只需要該函數(shù)的輸入?yún)?shù)和返回值與函數(shù)式接口中的抽象方法一致即可。例如上述代碼中的String::trim以及System.out::println。

擴(kuò)展接口

如果上述四大接口不能滿足需求,還有其他一些內(nèi)置接口可以選用,具體如下,就不一一列舉了其使用方法了。


其他函數(shù)式接口
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容