Four functional interfaces
lambda expression, chain programming, functional interface, Stream, streaming calculation
Functional interface: an interface with only one method
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
//Super multi FunctionalInterface
//Simplify the programming model, and apply a lot at the bottom of the new version of the framework
//foreach (functional interface for consumers)
Functional interface Function
package com.xizi.function;
import java.util.function.Function;
public class Test1 {
public static void main(String[] args) {
// Function<String,String> function = new Function<String,String>(){
// @Override
// public String apply(String s) {
// return s;
// }
// };
// lambda简化
Function<String,String> function =( s)->{return s;};
System.out.println(function .apply("abc"));
}
}
The assertion interface has an input parameter and the return value can only be a boolean
package com.xizi.function;
import java.util.function.Predicate;
public class Test2 {
public static void main(String[] args) {
//判断字符串是否为空
// 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(" "));
}
}
Consumer consumer interface only input without return value
package com.xizi.function;
import java.util.function.Consumer;
public class Test3 {
public static void main(String[] args) {
Consumer<String> consumer = new Consumer<String>(){
@Override
public void accept(String string) {
System.out.println(string);
}
};
Consumer<String> consumer2 = (string)->{System.out.println(string);};
consumer.accept("ava");
consumer2.accept("ava");
}
}
Supplier supply interface has no parameters and only return values
@FunctionalInterface
public interface Supplier<T> {
T get();
}
package com.xizi.function;
import java.util.function.Supplier;
public class Test4 {
public static void main(String[] args) {
// Supplier supplier = new Supplier<Integer>(){
// @Override
// public Integer get() {
// return 12546;
// }
// };
Supplier supplier =()->{ return 12546;};
System.out.println(supplier.get());
}
}