美文网首页
Java8 中内置的函数式接口

Java8 中内置的函数式接口

作者: Lost_09090 | 来源:发表于2019-10-28 15:03 被阅读0次
    1. Predicate<T>

      谓词

      1. test(T):boolean

        计算给定的谓词的值

        Predicate<Long> predicate = (l) -> l > 0L;
        predicate.test(3L); // true
        
      2. and(Predicate<? super T>):Predicate<T>

        返回一个与原谓词与关系组合后的谓词

        Predicate<Long> predicate = (l) -> l > 0L;
        predicate.and(l -> l < 10L).test(3L); // true
        predicate.and(l -> l > 10L).test(3L); // false
        
      3. or(Predicate<? super T>):Predicate<T>

        返回一个与原谓词或关系组合后的谓词

        Predicate<Long> predicate = (l) -> l == 0L;
        predicate.or(l -> l > 10L).test(15L); // true
        
      4. negate():Predicate<T>

        返回一个与原谓词非关系的谓词

        Predicate<Long> predicate = (l) -> l > 0L;
        predicate.negate().test(5L); // false
        
      5. isEqual(Object):Predicate<T>

        返回一个判断两个参数是否相等的谓词

        Predicate<String> predicate = Predicate.isEqual("string");
        predicate.test("what"); // false
        
    2. Function<T, R>

      函数

      1. apply(T):R

        给定参数应用于指定的函数. 返回一个结果

        Function<Long, String> function = String::valueOf;
        function.apply(40L); // "40"
        
      2. compose(Function<? super V, ? extends T>):Function<V, R>

        在原函数执行前执行给定参数的函数, 返回组合后的函数

        Function<Long, String> function = String::valueOf;
        String apply = function.compose(l -> (long) l.hashCode() + 30L).apply(40L); // 40
        
      3. andThen(Function<? super V, ? extends T>):Function<V, R>

        在原函数执行后执行给定参数的函数, 返回组合后的函数

        Function<Long, String> function = String::valueOf;
        Long apply1 = function.andThen(l -> (long) l.hashCode() - 30L).apply(100L); // 48595
        
      4. identity():Function<T, T>

        返回一个始终返回输入参数的函数

        Function<Object, Object> identity = Function.identity();
        Object string = identity.apply("String"); // "String"
        
    3. Supplier<T>

      提供者

      1. get():T

        根据给定的类属性产生一个对象

        Supplier<Student> supplier = Student::new;
        Student student = supplier.get();
        
    4. Consumer<T>

      消费者

      1. accept(T):void

        对给定的参数进行一系列预定义的流程处理

        Consumer<Student> consumer = student -> student.setName("peter");
        consumer.accept(new Student());
        
      2. andThen(Consumer<? super T>):Consumer<T>

        在对给定的参数处理后再进行后续的流程处理. 返回组合流程后的消费者

        Consumer<Student> consumer = student -> student.setName("peter");
        consumer.andThen(System.out::println).accept(new Student());
        

    相关文章

      网友评论

          本文标题:Java8 中内置的函数式接口

          本文链接:https://www.haomeiwen.com/subject/oedbvctx.html