Answer the question
In order to leave comments, you need to log in
How to implement higher order functions in Java?
I have a class, most of the methods differ only by calling different functions at the same point:
public MatrixDto getContent (...) {
...
final List<Content> list = <b>contentDao.x(a, b);</b>
...
return matrixDto;
}
Answer the question
In order to leave comments, you need to log in
Through interfaces, such as Callable .
public MatrixDto getContent(Callable<List<Content>> function) {
...
final List<Content> list = function.call();
...
}
// а вызывать так:
getContent(new Callable<List<Content>>() {
public List<Content> call() { return contentDao.x(a, b); }
});
Have a look at Google Guava docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Function.html
Although I increasingly think that higher-order functions in Java will not lead to anything good, they will only clutter code due to the verbosity of the language. Almost always, imperative code in Java looks better and more understandable than the so-called. "functional".
Try to compare and you will understand. A mess of anonymous classes is not the best code to read and understand.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question