<> Four functional interfaces

Programmers in the new era :lambda expression , Chain programming , Functional interface ,Stream Flow calculation

Functional interface : There is only one method interface
@FunctionalInterface public interface Runnable { public abstract void run(); }
// Super many FunctionalInterface // Simplified programming model , It is widely used in the new version of the framework ! //foreach( Functional interface for consumer types )

Code testing :

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's a functional interface sure use lambda Expression simplification */ public class Demo01 { public
static void main(String[] args) { // Tools : 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 : Input only , 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 also works Consumer, Self written examples :
// There are input parameters and no return values list.forEach(x->{ System.out.println(x); });
There are some references , no return value !

Supplier Supply interface

package com.function; import java.util.function.Supplier; /** * Supplier
Supply interface , There are 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 some references , Return value
* Deterministic interface : There are some references , Returns a Boolean value
* Consumer interface : There are some references , no return value
* Supply interface : No reference , There is a return value

Technology
©2019-2020 Toolsou All rights reserved,
【Java8 New features 1】Lambda Expression summary What is a process , Concept of process ?hdfs dfs Common basic commands java When creating objects, you must _Java Basic test questions Generation of random numbers + Figure guessing game When will we enter the old generation ?HDFS Common commands ( summary ) It is never recommended to spend a lot of time studying for employment pythonmacOS Big Sur The installation could not be completed Big Sur Why can't I install it ?Python pyttsx3| Text reading ( Various languages )