With the introduction of lambdas/predicates and functional programming in
Java 8 you get (almost) infinite possibilities to solve any problem. For each problem you'll have a bunch of non-functional and functional solutions. How do you know which one will be the best one (as in most performant one)?
For example, suppose this simple problem: print all the odd numbers in a list greater than 15 without any duplicates. I can at least think about 2 solutions:
And from these solutions you could create 2 other alternatives by just switching the filter statements. And maybe another alternative could be to combine both filter statements into one:
When I was at Devoxx 2014, I attended a few functional programming presentations. In one of them a very complex problem (about movies and actors, I believe) was solved using functional programming in Java. The Java 8 code of the 1st solution looked great, (even more important) made sense and returned the correct result almost instantly, so everything seemed just fine. But when a very big dataset was used, the program took minutes to complete with exactly the same code.
And somewhat related: does the compiler currently perform optimizations for you (or maybe in the future)? Just like when you write a sql query it's often optimized by a query optimizer.
Kind regards,
Roel