Java8函數筆記

Predicate

函數式接口:Predicate<T>
函數描述符:T -> boolean
原始類型特化:IntPredicate, LongPredicate, DoublePredicate

Predicate接口需要實現test()方法,返回boolean類型;
boolean test(T t);
另外有三個default方法

//&&,兩個都是true才返回true
default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }
//||,有一個true就返回true
default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }
// !,取反
default Predicate<T> negate() {
        return (t) -> !test(t);
    }
Predicate<String> predicate = s -> "1".equals(s) || "2".equals(s);
Predicate<String> other= s -> "3".equals(s) || "4".equals(s);
//
Predicate<String> and = predicate.and(other);
Predicate<String> or = predicate.or(other);
Predicate<String> negate = predicate.negate();
System.out.println("test:" + predicate.test("3")+ "==and==" + and.test("3") + "==or==" + or.test("3") + "==negate==" + negate.test("1"));

打印的Log

test:false==and==false==or==true==negate==false

Consumer

函數式接口:Consumer<T>
函數描述符:T -> Void
原始類型特化:IntConsumer, LongConsumer, DoubleConsumer

Consumer接口需要實現accept()方法,沒有返回值。
void accept(T t);
一個default方法

//先執行當前Consumer對象的accept()方法,緊接着調after的accept()方法。
default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
Consumer<String> consumer = System.out::println;
        Consumer<String> after = s -> System.out.println(s + "你好");

        consumer.accept("Hello World!");

        Consumer<String> andThen = consumer.andThen(after);
        andThen.accept("美女");

打印Log

Hello World!
美女
美女你好

Function

函數式接口:Function<T,R>
函數描述符:T -> R
原始類型特化:

IntFunction<R>,
IntToDoubleFunction,
IntToLongFunction,
LongFunction<R>,
LongToDoubleFunction,
LongToIntFunction,
DoubleFunction<R>,
ToIntFunction<T>,
ToDoubleFunction<T>,
ToLongFunction<T>

Function接口實現apply()方法,
R apply(T t);
一個default方法

//before調apply方法的返回值傳入當前對象的apply方法中返回,當然before返回值必須是當前對象的參數類型
 default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }
Function<String, String> function = s -> s + ",你好";
        Function<String, String> before = s -> s + s;
        String apply = function.apply("小子");
        System.out.println(apply);

        Function<String, String> compose = function.compose(before);
        System.out.println(compose.apply("包子"));

打印Log

小子,你好
包子包子,你好

Supplier

函數式接口:Supplier<T>
函數描述符:() -> T
原始類型特化:BooleanSupplier,IntSupplier, LongSupplier, DoubleSupplier

Supplier只有一個實現方法
T get();

Supplier<String> supplier = () -> "100";
        String s = supplier.get();
        System.out.println(s);

        Supplier<Person> personSupplier = () -> {
            Person person = new Person();
            person.name = "小明";
            return person;
        };
        Person person = personSupplier.get();
        System.out.println(person.name);

打印Log

100
小明

還有。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章