fb_pixel
Description
Lambdas are the anonymous function which are useful when you want to pass around fragments of reusable functionality. For example, passing functions into other functions.
Attachments
No attachments
Info
Description
A typical implementation of an anonymous class (a single method interface) in Java pre-8, might look something like this. The anonymousClass method is calling the waitFor method, passing in some implementation of Condition; in this case, it’s saying, wait for some server to shutdown:

Typical usage of an anonymous class:

void anonymousClass() {
 final Server server = new HttpServer();
 waitFor(new Condition() {
 @Override
 public Boolean isSatisfied() {
 return !server.isRunning();
 }
 });
}


Equivalent functionality as a lambda:

void closure() {
 Server server = new
HttpServer();
 waitFor(() -> !server.
isRunning());
 }


Where in the interest of completeness, a naive polling waitFor method might look like this:

class WaitFor {
 static void waitFor(Condition
condition) throws
 InterruptedException {
 while (!condition.isSatisfied())
 Thread.sleep(250);



on December 04th, 2019 (9:09 pm)
All coments
Wow, brilliant explanation.
on December 19th, 2019 (2:40 pm)