The functional formula of the new features of java1.8 _ learning experience

! ! ! Disclaimer: This article is a personal learning experience and does not represent the actual situation. If there are errors, please point out that the
functional interface mainly includes the following interfaces and other derived interfaces:

1.Consumer<T> 代表了接受一个输入参数T并且无返回的操作
2.Function<T,R>接收一个参数T,返回R
3.Predicate<T>接收一个参数T返回一个boolean类型的值
4.Supplier<T>无参数

This article mainly explains the first three
learning functional styles. It is best to master the following knowledge points. It does not matter, but learning is more difficult:

1. The default method of default modification in
java1.8 2. The lambda expression in
java 1.8 3. java1. 8 method references

1.Consumer< T>

Java source code: The default method foreach parameter in the Iterable interface is Consumer type
code 001

    default void forEach(Consumer<? super T> action) {
    
    
        Objects.requireNonNull(action);
        for (T t : this) {
    
    
            action.accept(t);
        }
    }

The calling method is as follows:
code 002

        List<User> userList=new ArrayList<>();//数据省略不写
        //1.普通for循环
        for(int i=0;i<userList.size();i++){
    
    
            System.out.println(userList.get(i).getName());
        }
        //2.foreach
        for(User user:userList){
    
    
            System.out.println(userList.get(i).getName());
        }
        //3.重点:最复杂,新手最易懂的方式
        userList.forEach(user->{
    
    
            System.out.println(user.getName());
        });
        //4.函数式参数只有一句代码时可以简化去掉大括号,去掉分号
        userList.forEach(user -> System.out.println(user.getName()));

The code printing results of all the above methods are consistent.
According to the source code analysis of 001 above the code, it can be seen that the parameter of action.accept(t); in the for loop is user in code 002 , and the name of user
starts at random, which is equivalent to the functional code in code 002 . Quoting, System.out.println(user.getName()) is a functional parameter. Variables in other places can be referenced in the code block of the functional parameter. (Note! When quoting, all variables outside the code block are equivalent to being final modification), as follows:
Insert picture description here
direct assignment will compile an error!
The test code is as follows:

    public static void main(String[] args) {
    
    

        User user=new User(1,"Jack");
        test1(user,u->{
    
    
            System.out.println("我的朋友是:"+u.getName());
        });
        test1(user,u->{
    
    
            System.out.println("我的哥哥是:"+u.getName());
        });
    }
    public static void test1(User user,Consumer<? super User> consumer){
    
    
        System.out.println("----这里是大量逻辑代码1");

        consumer.accept(user);
        System.out.println("----这里是大量逻辑代码2");
    }

operation result:
Insert picture description here

2.Function<T,R>

Function has one more return value than Consumer. The
test code is as follows:

    public static void main(String[] args) {
    
    
        callTest2();
    }
    public static void callTest2(){
    
    
        List<User> userList=new ArrayList(){
    
    {
    
    
            add(new User(11,"Jack"));
            add(new User(12,"zhangsan"));
            add(new User(23,"lisi"));
            add(new User(34,"wangwu"));
        }};
        test2(userList,user -> {
    
    
            if(user.getAge()<18){
    
    
                return "未成年";
            }
            return "已经成年";
        });
    }
    public static void test2(List<User> list, Function<? super User,? extends String> function){
    
    
        System.out.println("----这里是大量逻辑代码1");
        list.forEach(user->{
    
    
            System.out.println(user.getName()+":"+function.apply(user));
        });
        System.out.println("----这里是大量逻辑代码2");
    }

Operation result: to
Insert picture description here
be continued

Guess you like

Origin blog.csdn.net/qq_41844287/article/details/114240595