1、Function 函数型接口
package com.huang.func;
import java.util.function.Function;
/**
* Function函数式接口
*/
public class Demo01 {
public static void main(String[] args) {
// Function function = new Function<String,String>(){
// @Override
// public String apply(String s) {
// return s;
// }
// };
Function function = (str) -> {return str;};
System.out.println(function.apply("111"));
}
}
2、Predicate 断定型接口
package com.huang.func;
import java.util.function.Predicate;
/**
* Predicate 断定型接口
*/
public class Demo02 {
public static void main(String[] args) {
// Predicate 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("123"));
}
}
Consumer 消费型接口
![](https://img2020.cnblogs.com/blog/1564589/202008/1564589-20200821200419706-1834601102.png)
package com.huang.func;
import java.util.function.Consumer;
/**
* Consumer 消费型接口 只有输入,没有返回值
*/
public class Demo03 {
public static void main(String[] args) {
// Consumer consumer = new Consumer() {
// @Override
// public void accept(Object o) {
// System.out.println(o);
// }
// };
Consumer<String> consumer = (str) -> {
System.out.println(str);
};
consumer.accept("123");
}
}
Supplier 供给型接口
![](https://img2020.cnblogs.com/blog/1564589/202008/1564589-20200821200854518-462282302.png)
package com.huang.func;
import java.util.function.Supplier;
/**
* Supplier 供给型接口 没有输入参数,只有返回值
*/
public class Demo04 {
public static void main(String[] args) {
// Supplier supplier = new Supplier<Integer>() {
// @Override
// public Integer get() {
// System.out.println("get()");
// return 1024;
// }
// };
Supplier<Integer> supplier = () -> {
System.out.println("get()");
return 1024;
};
supplier.get();
}
}