<> Four functional interfaces
Programmers of the new era :lambda expression , Chain programming , Functional interface ,Stream Flow calculation
Functional interface : Interface with only one method
@FunctionalInterface public interface Runnable { public abstract void run(); }
// Super much FunctionalInterface // Simplified programming model , It is widely used at the bottom of the new version of the framework ! //foreach( Functional interface of consumer type )
Code test :
Function Functional interface
package com.function; import java.util.function.Function; /** * Function
Functional interface , There is an input parameter , There is an output * As long as it is a functional interface sure use lambda Expression simplification */ public class Demo01 { public
static void main(String[] args) { // Tool class : Output input value // Function function = new
Function<String,String>() { // @Override // public String apply(String o) { //
return o; // } // }; //System.out.println(function .apply("asd")); Function<
String,String> function= (str)->{return str;}; System.out.println(function.apply
("asd")); } }
Output results :asd
Deterministic interface : There is an input parameter , The return value can only be Boolean !
package com.function; import java.util.function.Predicate; /** *
Deterministic interface , There is an input parameter , The return value can only be Boolean ! */ public class Demo02 { public static void main(String
[] args) { // Determine whether the string is empty // Predicate<String> predicate = new Predicate<String>()
{ // @Override // public boolean test(String str) { // return str.isEmpty(); //
} // }; Predicate<String> predicate =(str)->{ return str.isEmpty(); }; System.
out.println(predicate.test("asd")); } }
Output results :false
Consumer Consumer interface
package com.function; import java.util.function.Consumer; /** * Consumer
Consumer interface : Only input , no return value */ 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); // } // }; // Functional interface
Consumer<String> consumer = (str)->{ System.out.println(str); }; consumer.accept
("ssssssssssssssss"); } }
Output results :sssssssssssss
list.foreach In fact, it is also used Consumer, Write your own examples :
// There are input parameters and no return value list.forEach(x->{ System.out.println(x); });
There are input parameters , no return value !
Supplier Supply type interface
package com.function; import java.util.function.Supplier; /** * Supplier
Supply type interface , No parameters , Only return value */ public class Demo04 { public static void main(String[] args)
{ // Supplier<String> supplier = new Supplier<String>() { // @Override //
public String get() { // System.out.println("Get"); // return "1024"; // } // };
Supplier<String> supplier = () ->{ return "1024"; }; System.out.println(supplier
.get()); } }
Output results :1024
summary
* Functional interface : There are input parameters , Return value again
* Deterministic interface : There are input parameters , Returns a Boolean value
* Consumer interface : There are input parameters , no return value
* Supply type interface : No input parameter , There is a return value
Technology