I have quickly read over the Oracle Lambda Expression documentation.
This kind of example has helped me to understand better, though:
//Old way: List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7); for(Integer n: list) { System.out.println(n); } //New way: List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7); list.forEach(n -> System.out.println(n)); //or we can use :: double colon operator in Java 8 list.forEach(System.out::println);
Still, I don't understand why it's such an innovation. It's just a method that dies when the "method variable" ends, right? Why should I use this instead of a real method? Which would be better option in terms of performance. Lambda or simple loop.
Oracle claims that use of lambda expressions also improve the collection libraries making it easier to iterate through, filter, and extract data from a collection. In addition, new concurrency features improve performance in multicore environments.
Again, the for- loop is faster that the sequential stream operation, but the difference on an ArrayList is not nearly as significant as it was on an array.
Advantages of the streams:Your stream-handling code doesn't need to know the source of the stream or its eventual terminating method. Streams can succinctly express quite sophisticated behavior. Streams can be a replacement for looping because they allow for the processing of a sequence of data (similarly to a loop).
My advice would be:
Use the style that you and your coworkers agree is most maintainable.
If you and your colleagues are not yet comfortable with lambdas, keep learning.
Don't obsess over performance. It is often not the most important thing.
Generally speaking, lambdas and streams provide a more concise and (once everyone is up to speed) more readable way of expressing this kind of algorithm. Performance is not the primary goal.
If performance does become an issue, then the standard advice is to code, test, benchmark, profile and optimize. And do it in that order! You can easily waste a lot time by optimizing at the coding stage, or by optimizing code that has minimal impact on overall application performance.
In this specific example, the performance difference is going to be too small to measure. And if you scaled up to a list of millions of elements, the performance will be dominated by the time taken to build the list and write the numbers. The different ways of iteration will only contribute a small part to the overall performance.
And for folks, who (despite all of the above) still want to know whether it is faster to use a lambda or a conventional loop, the best general answer is:
"It depends on all sorts of factors that 1) are not well understood, and 2) liable to change as Java compiler technology evolves.
We could give you an answer for a specific example with a specific Java major/minor/patch release, but it would be unwise to generalize.
Why should I use this instead of a real method?
You should not. Use the approach which you like more.
As for performance, I guess, all these versions are roughly equally fast. Here I/O operation (println
) is much slower than all possible overhead of calling lambda or creating an iterator. In general forEach
might be slightly faster as it does everything inside the single method without creating the Iterator
and calling hasNext
and next
(which is implicitly done by for-each loop). Anyway, this depends on many factors, such as how often you call this code, how long your list is, whether JIT compiler managed to devirtualize the iterator and/or lambda, and so on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With