Posts

Showing posts from 2016

Java 8 lambda expression & method reference made easy

Lambda expression facilitates functional programming in Java. 

Lambda expression is a function which can be created without belonging to any class.Lambda expression can be passed around as if it was an object and executed on demand as method reference.Lambda expressions are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. Following demonstrates serious ways of using lamda expression.
Method reference:

A method reference is the shorter more concise syntax for a lambda expression that executes just ONE method.

There are four types of method references:
A method reference to a static method.A method reference to an instance method of an object of a particular type.A method reference to an instance method of an existing object.A method reference to a constructor.
Sample program:

// Functional interfaces are heart of lambda expression
@FunctionalInterface
interface FuncIface {
    public void referenceDemo(); //Only one a…