I've already looked here, that works for an argument less method list containing Runnables. I need to have Consumer
in my case. This is what I have so far:
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.function.Consumer;
public class experiment {
private static List<Consumer<String>> activities;
public experiment (){
activities = ImmutableList.of(
(userId) -> this::bar, // throws error
(userId) -> this::foo);
}
public void bar(String x) {
System.out.println(x);
}
public void foo(String x) {
System.out.println(x);
}
public static void main(String []args) {
for (int i=0; i<2; i++) {
activities.get(i).accept("activity #" + i);
}
}
}
The error I see is void is not a functional interface
.
I don't understand why I'm getting that, both bar
and foo
implement the accept method of the Consumer interface. Is there something I'm overlooking here?