函数接口
@FunctionalInterface
public interface DemoFunction<T> {
T data();
}
引用函数接口的方法
public class Demo<T extends Collection> {
public Demo addData(T t, DemoFunction function) {
if (t.contains(function.data())) {
System.out.println("data:" + function.data() + " repetition not add");
} else {
t.add(function.data());
System.out.println("add success data:" + function.data());
}
return new Demo();
}
}
调用主方法
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Demo demo = new Demo();
demo.addData(list, () -> "666")
.addData(list, () -> "666")
.addData(list, () -> "5555")
.addData(list, () -> 7895)
.addData(list, () -> true)
.addData(list, () -> true);
System.out.println("list = " + list);
}
下面是运行效果,英语请忽略不计,纯散装的
|