I have a function that iterates over a heavy data set, receives a callback (Google's Guava Function) and runs the callback on every item of the data set:
void processData(..., Function<Item, Void> callback) {
...
for (Item item : data) {
callback.apply(item);
}
}
Using this function, I would like to pass a callback that adds all items to a list or a map:
List<Item> itemList;
processData(..., new Function<Item, Void)() {
@Override public void apply(Item item) {
itemList.add(item);
}
});
However, it appears I can't do that, since itemList is not final, and cannot be final by definition.
Is there a solution to this case? Or perhaps this entire pattern is wrong?