@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
Consumer一个函数式接口
怎么去使用呢
Consumer<String> con = ((str)->{
System.out.println(str);
});
现在定义出来一个函数接口,Consumer<String> 所需要参数为String类型 又函数式中的str接受
怎么去调用函数体呢
con.accept("consumer");
通过默认的accept就可以进项调用
同理Function Supplier也是一样的
Supplier
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
Supplier<String> sup = (()->{
return "1";
});
System.out.println(sup.get());
Function
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
Function<String, Integer> fun = ((str)->{
return Integer.valueOf(str);
});
System.out.println(fun.apply("我是function"));
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。