25, JUC: consumer interface, supply interface

Watch the video of the learning process: [Crazy God Says Java]
https://www.bilibili.com/video/BV1B7411L7tE?p=13
Welcome everyone to support Oh, very conscientious teacher!

1. Consumer interface:

Insert picture description here
java code

package com.function;

/**
 * Created by zjl
 * 2020/11/25
 **/

import java.util.function.Consumer;

/*** Consumer 消费型接口: 只有输入,没有返回值 */
public class Demo03 {
    
    

    public static void main(String[] args) {
    
    
        //方式一
        Consumer<String> consumer = new Consumer<String>() {
    
    
            @Override
            public void accept(String str) {
    
    
                System.out.println(str);
            }
        };
        consumer.accept("abc");


        //方式二
        Consumer<String> consumer1 = (str2)->{
    
    System.out.println(str2);};
        consumer1.accept("bcd");

    }
}

Output result:
Insert picture description here

2. Supply interface

Insert picture description here
java code example:

package com.function;

import java.util.function.Supplier;

/**
 * Created by zjl
 * 2020/11/25
 **/
/**
 *  Supplier 供给型接口 没有参数,只有返回值
 */
public class Demo04 {
    
    
    public static void main(String[] args) {
    
    
        //方式一
        Supplier<Integer> supplier = new Supplier<Integer>() {
    
    
            @Override
            public Integer get() {
    
    
                return 1024;
            }
        };
        System.out.println(supplier.get());



//        方式二
        Supplier supplier1 = ()->{
    
    return 1024;};
        System.out.println(supplier1.get());
    }

}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41347385/article/details/110134465