当前位置:网站首页>Four functional interfaces (required)

Four functional interfaces (required)

2022-06-22 06:18:00 Kuxiaoya

In functional interface , Look at the source code :

1、 Super many FunctionalInterface
2、 Simplify the programming model , It is widely used at the bottom of the new version of the framework !

@FunctionalInterface
public interface Runnable {
    
    public abstract void run();
}

What are the four functional interfaces ?

Function Functional interface : There is an input parameter , There's an output

Predicate Assertive interface : There is an input parameter , The return value can only be Boolean value !

Consumer Supply type interface : Only the input , no return value

Supplier Supply type interface : No parameters , Only the return value

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-M4ZBJQwQ-1655057388443)(C:\Users\38492\AppData\Local\Temp\1655055413345.png)].

Code testing :

Function Functional interface

package function;

import java.util.function.Function;

/** * Function  Functional interface , There is an input parameter , There's an output  *  As long as it is   Functional interface   It can be used Lambda Expression simplification  */
public class Demo1 {
    
    public static void main(String[] args) {
    
        //  Tool class : Output input value 
// Function function = new Function<String,String>() {
    
// @Override
// public String apply(String s) {
    
// return s;
// }
// };


        Function function =  (str)-> {
    
            return str;
        };
        System.out.println(function.apply("asd"));

  }
}

Predicate Assertive interface

package function;

import java.util.function.Predicate;

/** *  Assertive interface : There is an input parameter , The return value can only be   Boolean value ! */
public class Demo2 {
    
    public static void main(String[] args) {
    
        // Determines if the string is empty 
// Predicate<String> predicate = new Predicate<String>() {
    
// @Override
// public boolean test(String s) {
    
// return s.isEmpty();
// }
// };

        Predicate<String> predicate = (str)-> {
    
            return str.isEmpty();
        };
        System.out.println(predicate.test(""));
    }
}	

Consumer Consumer interface

package function;

import java.util.function.Consumer;

/** * Consumer  Consumer interface : Only the input , no return value  */
public class Demo3 {
    
    public static void main(String[] args) {
    
// Consumer<String> consumer = new Consumer<String>() {
    
// @Override
// public void accept(String s) {
    
// System.out.println(s);
// }
// };

        Consumer<String> consumer = (str)->{
    
            System.out.println(str);
        };
        consumer.accept(" Kuxiaoya !");
    }
}

Supplier Supply type interface

package function;

import java.util.function.Supplier;

/** * Supplier  Supply type interface : No parameters , Only the return value  */
public class Demo4 {
    
    public static void main(String[] args) {
    
// Supplier<String> supplier = new Supplier<String>(){
    
// @Override
// public String get() {
    
// return " Ha ha ha ";
// }
// };

        Supplier<String> supplier =()->{
    
            return " Kuxiaoya !";
        };
        System.out.println(supplier.get());
    }
}

原网站

版权声明
本文为[Kuxiaoya]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220609550242.html