My collect()
function calls Foo.f()
. I would like to make Foo.f()
itself a parameter of my function. Is this possible in Java?
- How can I pass either
Foo.f()
orFoo.g()
(or any other function ofFoo
that returns aString
) to my function? - Is there an already existing function which walks a collection and collects the result of calling a method of every collection item?
.
class Foo {
public String f() { return "f"; }
public String g() { return "g"; }
// ...
}
public List<String> collect(List<Foo> foos)
{
List<String> result = new ArrayList<String>();
for (final Foo foo: foos) {
result.add(foo.f()); // I want Foo.f to be a parameter
}
return result;
}
Update
I would like to point out the fact that I am not merely calling the same function but rather the member function f
for all items of the List<Foo>
collection.