• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anonymous new function can be replaced by Lambda?  RSS feed

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My IDE suggests that this can be converted into a Lambda expression.



I only use it like this...



I know that I'm supposed to operate on an Interface, which is function in this case. It's not that I'm being lazy, but could someone please show me how it's done?

Thanks in advance!
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Timothy,

these lambda's can sometimes be very confusing, for me too, and then there's
a plethora of functional interfaces as well.

But look at the following example:


In your case, you would get:

But you do not need to specify some lambda in one line. Look at this:

Then this will also work:

You see, quite some flexibility, but if you're not doing things like these
on a day-to-day basis, then, yes, forgetting it and getting confused
is part of the job.

Greetz,
Piet
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that whenever you have a lambda of the form x -> foo.doSomething(x) you can instead use foo::doSomething.

Instead of saying

Consumer<Object> print = (o) -> System.out.println(o);,

use

Consumer<Object> print = System.out::println;
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same works for instance methods:
(X x) -> x.doSomething()
X::doSomething

An example:
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16007
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timothy Sam wrote:My IDE suggests that this can be converted into a Lambda expression.
...
I know that I'm supposed to operate on an Interface, which is function in this case. It's not that I'm being lazy, but could someone please show me how it's done?

Doesn't your IDE also have a way to automatically change it into a lambda expression?

It would look like this - shorter and clearer, without all the boilerplate for an anonymous inner class.

 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what would be a practical advantage of defining it this way above the classical
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not much, I would prefer writing regular methods. Use functional interfaces as method parameters, or as instance fields if you need to store an operation for later use. I don't think they have much use for direct assignment.
 
Thomas Gard
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:The same works for instance methods:
(X x) -> x.doSomething()
X::doSomething

An example:


Every new idiom that shows up as a shortcut requires an enormous effort on my part to [re]develop my intuition set.

I'm at that odd stage in having to defeat decades of OO thinking whenever I read this stuff.

I mean, ...still reads to me as pure gobbledygook.
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, functional programming is very difficult when you're used to imperative languages. I can really recommend trying out Haskell, it completely changed how I think about writing programs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!