Java8函数笔记

来源:互联网 发布:dive into python 2 编辑:程序博客网 时间:2024/06/05 16:44

Predicate

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

Predicate接口需要实现test()方法,返回boolean类型;
boolean test(T t);
另外有三个default方法

//&&,两个都是true才返回truedefault Predicate<T> and(Predicate<? super T> other) {        Objects.requireNonNull(other);        return (t) -> test(t) && other.test(t);    }
//||,有一个true就返回truedefault 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小明

还有。。。

0 0
原创粉丝点击