• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anonymous new function can be replaced by Lambda?

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The same works for instance methods:
(X x) -> x.doSomething()
X::doSomething

An example:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic