What I want to do is to easily pass in an actual method object to another method. This is so I can create a generic Joiner function that joins on a given method of a list of objects without the danger of just passing in a string that represents the method. Is this possible in Java so that I can have syntax like:
joinOnMethod(String, Collections<T>, Method) e.g.
joinOnMethod(", ", someList, T.getName())
Where T.getName() passes the getName() method into be called on each object in the list instead of passing in the return type of the method itself.
Hope this is clear enough for my needs,
Alexei Blue.
There’s no first-class function support in Java directly. Two options:
Create a type with a single member, like this:
Then you can use an anonymous inner class, like this:
Of course you can extract that function as a variable: