• Post Reply Bookmark Topic Watch Topic
  • New Topic

Core Java question: benefits of lambda expressions

 
Ferdinand Victorinus
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the most significant benefits of lambda expressions?
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there are two ways of looking at that. From a purely practical perspective, you don't have to do as much typing. For example,



is less typing than



But I like to think about it more philosophically. The lambda syntax better expresses the intent of the programmer. When I have a task, then I think "when it is invoked, then call doWork 1000 times". I do not think that I want to create an anonymous class that implements the Runnable interface whose run method does ...". All that stuff with the anonymous class and the interface and the run method is just implementation detail. With the old way, the detail drowned out the important part. With lambda expressions, the unimportant part fades in the background, and you write and read the code that matters.

Cheers,

Cay
 
Steve Beckle
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think one big advantage is the functional programming you can achieve. You can now create functional libraries and use individual methods as needed. For example:



Now, you can move Helper to some other .jar file and recompile as needed if the logic changes, and leave your original code in App{} unchanged.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!