• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mastering Lambdas  RSS feed

 
Arjun Shastry
Ranch Hand
Posts: 1903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maurice,
We are in a process of upgrading from Java 6 to Java 8. What kind of code we should be looking at to re-factor by Lambda expressions? What are the benefits of using Lambdas?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really need to refactor code which is working correctly already?

You can replace loops with Streams. You can replace many anonymous classes with λs. ActionListener for example has 1 method so it counts as a functional interface and can often be replaced with a λ.
Just as good, Java8 has replacements for Calendar and java.util.Date.
 
Arjun Shastry
Ranch Hand
Posts: 1903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Streams, new Calendar etc if used properly, improve performance over 'for' loop or existing Calendar class?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are not really for improving performance, but for simplifying the code you have to write. Also by using a Stream you can let the JVM do the optimisation and you may get different optimisations on different machines (1‑core, 2‑core, etc).

The replacements for Calendar which you can read about in the Java™ Tutorials are not about performance either, but about a decent interface rather than the horrible interface of the older classes.
 
Maurice Naftalin
Author
Greenhorn
Posts: 20
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arjun

I agree with Campbell: you have to make a case for refactoring code that is working satisfactorily. The case might be easy to make if you are planning, say, to extend it substantially, but it still has to be made. An easy refactoring is to use lambdas instead of anonymous inner class implementations of single-abstract-method interfaces. Next would come replacement of loops processing of collections by stream code, especially if you need to improve performance and you see an opportunity to use parallel streams for this purpose. As for date/time processing, I would be very hesitant to convert existing code using java.util into java.time code; although java.time provides an infinitely better API than the java.util classes, the programming model is quite different, so the conversion would likely be a lot of work. But java.time is a joy to work with, so I would definitely use it in any new development.

Best regards
Maurice
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have Date/Calendar working, you deserve a medal, and why refactor it? You won't get an advantage.
If you have anonymous classes working, why refactor them?
If you have a while loop, that will run single threaded. In Java11 all streams will be refitted to detect the number of cores in the processor automatically and calculate whether it is worth parallelising the Stream. Like a JIT compiler, that will enable the fastest execution, but depends on refactoring all your loops into Streams.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:In Java11 . . .
That's my story and I'm sticking to it

It is only a guess, but it might be correct.
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering your track record, you will be...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!