尝试使用jdk8函数式接口与方法引用

在之前学习java时,基本没有使用到函数式接口与方法引用这一部分特性,只用过lambda表达式作为参数传递给方法使用。

java1.8有如下接口

函数式接口

使用

在这里尝试使用6种函数式接口,并且将方法引用传递给声名的接口对象。

接口名称接口作用
Function<T,R>T 作为输入,返回的 R 作为输出
Supplier没有输入 , R 作为输出
ConsumerT 作为输入 ,没有输出
PredicateT 作为输入 ,返回 boolean 值的输出
BinaryOperator两个 T 作为输入 ,T 同样是输出
UnaryOperator是 Function 的变种 ,输入输出者是 T
/**
 * Function<T,R> T 作为输入,返回的 R 作为输出
 * Supplier<R> 没有输入 , R 作为输出
 * Consumer<T> T 作为输入 ,没有输出
 * Predicate<T> T 作为输入 ,返回 boolean 值的输出
 * BinaryOperator<T> 两个 T 作为输入 ,T 同样是输出
 * UnaryOperator<T> 是 Function 的变种 ,输入输出者是 T
 */
public class jdk8Test {
    public static void main(String[] args){
        Function<Integer, UserJdk8Test> functionTest = Test::getUserById;
        Supplier<Test> supplierTest = Test::new;
        Consumer<Object> consumerTest = System.out::println;
        Predicate<UserJdk8Test> predicateTest = supplierTest.get()::equals;
        BinaryOperator<Integer> binaryOperatorTest = supplierTest.get()::addUserId;
        UnaryOperator<UserJdk8Test> unaryOperatorTest = supplierTest.get()::addUser;


        UserJdk8Test userJdk8Test = functionTest.apply(1);
        consumerTest.accept(supplierTest.get());
        consumerTest.accept(predicateTest.test(userJdk8Test));
        consumerTest.accept(binaryOperatorTest.apply(1,2));
        consumerTest.accept(unaryOperatorTest.apply(userJdk8Test));

        Test.userSupplier(consumerTest,supplierTest);
    }
    static class Test{
        static UserJdk8Test getUserById(Integer id){
            return new UserJdk8Test(id,id.toString());
        }
        boolean equals(UserJdk8Test userJdk8Test){
            return userJdk8Test.equals(userJdk8Test);
        }
        UserJdk8Test addUser(UserJdk8Test userJdk8Test){
            userJdk8Test.setId(userJdk8Test.getId()+1);
            userJdk8Test.setUsername(userJdk8Test.getId().toString());
            return userJdk8Test;
        }
        Integer addUserId(Integer a,Integer b){
            return a+b;
        }
        static void userSupplier(Consumer consumer,Supplier supplier){
            consumer.accept("userSupplier:"+supplier.get());
        }
        @Override
        public String toString(){
            return "Test";
        }
    }
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class UserJdk8Test{
        private Integer id;
        private String username;
        @Override
        public String toString(){
            return "[id="+id+",username="+username+"]";
        }
    }
}

# java 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×