Java 如何在Android API中将对象包装在lambda中<;24
我想迭代一个集合,作用于每个项Java 如何在Android API中将对象包装在lambda中<;24,java,android,java-8,functional-programming,Java,Android,Java 8,Functional Programming,我想迭代一个集合,作用于每个项 Collection<Listener> listeners = .... interface Listener { void onEventReceived(); void onShutDown(); } 我想抓住java8 lambdas,所以我声明了一个助手接口: interface WrapHelper<T> { void performAction(T item); } 我的问题是:我是否需要声明
Collection<Listener> listeners = ....
interface Listener {
void onEventReceived();
void onShutDown();
}
我想抓住java8 lambdas,所以我声明了一个助手接口:
interface WrapHelper<T> {
void performAction(T item);
}
我的问题是:我是否需要声明接口
WrapHelper
我自己,因为Android API中已经有一个用于此目的的类是的,你需要声明你的接口WrapHelper,因为我不知道Android API,但是你有权访问JDK8的java.util.function.Consumer
?这是对应于您的WrapHelper
的选项。仅举几个例子。
interface WrapHelper<T> {
void performAction(T item);
}
public void notifyListeners(WrapHelper<Listener> listenerAction) {
for (Listener listener : listeners) {
listenerAction.performAction(listener);
}
}
public void notifyEventReceived() {
notifyListeners(listener -> listener.onEventReceived());
}
public void notifyShutDown() {
notifyListeners(listener -> listener.onShutDown());
}