• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 8 Lambdas: old problems

 
Yvette Schat
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Richard,

I've been out of Java for a few years (since Java 5) due to an unlucky technology choice in our organisation

But now things are looking brighter since it is being picked up again and we'll migrate to Java...

Better late than never...

Can you brief me on what 'old problems' Lambdas/functional programming can solve?

Thank you,
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15715
73
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These two documents by Brian Goetz, the Project Lambda lead engineer at Oracle, explain very nicely what lambda expressions are and why they are useful:

State of the Lambda
State of the Lambda: Libraries Edition
 
Yvette Schat
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dankjewel

I will read them thoroughly.
 
Richard Warburton
Author
Greenhorn
Posts: 22
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yvette Schat wrote:Dear Richard,

I've been out of Java for a few years (since Java 5) due to an unlucky technology choice in our organisation

But now things are looking brighter since it is being picked up again and we'll migrate to Java...

Better late than never...

Can you brief me on what 'old problems' Lambdas/functional programming can solve?

Thank you,


Hi Yvette,

There are several questions on a related topic here - namely the underlying motivation behind lambda expressions themselves. I'm going to try and give a detailed answer here and then link the other threads to this answer.

You've probably had lots of times where you were processing a collection of things, and you wanted to build up a new collection afterwards. This meant that you wrote some boilerplate code everywhere along the lines of:



Now you would like to abstract out the code for this pattern, right? You don't want to have to write the same pattern of code everywhere. Even in Java 7 you could write a method like this:



Unfortunately when you wanted to define your transformer function, you would have to write an anonymous inner class. So even abstracting over the simplest behavioural patterns like this required 5 lines of boilerplate. Essentially you were back to square one. Now the advantage lambdas give you is being able to pass around some behaviour, eg a way of transforming a value into another value or a predicate on a value, in a really simple and clean way. For example the original code example could be rewritten much more concisely as follows:



So what we can now do is introduce useful abstractions in a way that was impractical to do before. All the new core Java 8 libraries, such as streams and collectors, follow this pattern of using lambdas to abstract over different behaviours.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!