Java – Lambda expressions

Previous
Next

Lambda expressions:

  • It is JDK 8 feature.
  • It is used to represent functional interface as an expression.

Functional interface:

  • An interface with single specification(abstract method).
  • Generally we implement interfaces with classes, inner classes or mainly anonymous inner classes.

Implement an interface with class:

interface Test
{
	void fun();
}
class Demo implements Test
{
	public void fun(){
		System.out.println("fun...");
	}
}
class Access 
{
	public static void main(String[] args) 
	{
		Test obj = new Demo();
		obj.fun();
	}
}

Implementing interface with inner class:

interface Test
{
	void fun();
}
class Access 
{
	class Demo implements Test
	{
		public void fun(){
			System.out.println("inner class - fun...");
		}
	}
	public static void main(String[] args) 
	{
		Test obj = new Access().new Demo();
		obj.fun();
	}
}

Implementing interface with anonymous inner class:

interface Test
{
	void fun();
}
class Access 
{
	public static void main(String[] args) 
	{
		Test obj = new Test()
		{
			public void fun()
			{
				System.out.println("Anonymous fun...");
			}
		};
		obj.fun();
	}
}

Writing above code with lambda:

interface Test
{
	void fun();
}
class Access 
{
	public static void main(String[] args) 
	{
		Test obj = ()->{
				System.out.println("Lambda fun...");
		};
		obj.fun();
	}
}

Advantages:

  • It is JDK8 feature.
  • It is very useful in collection library.
  • It saves lot of code in the implementation of functional interface(only one abstract method).
  • Java Lambda expression treated as a function, so compiler will not generate any class file.

Dis-advantages:

  • Anonymous inner class can implement an interface with more than one method. But lambda expression cannot do.
  • In the below code, we implement anonymous inner class with more than one specification.
interface Test
{
	void m1();
	void m2();
}
class Access 
{
	public static void main(String[] args) 
	{
		Test obj = new Test()
		{
			public void m1()
			{
				System.out.println("Anonymous m1...");
			}
			public void m2()
			{
				System.out.println("Anonymous m2...");
			}
		};
		obj.m1();
		obj.m2();
	}
}
  • Lambda expression can write the body for only one method.
  • If an interface contains more than one specification, we cannot go for lambda.
interface Test
{
	void fun();
}
class Access 
{
	public static void main(String[] args) 
	{
		Test obj = ()->{
				System.out.println("Lambda fun...");
		};
		obj.fun();
	}
}
  • In the above code, we didn’t mention the method name in lambda expression.
  • We can call the logic of that method directly.

Lambda expression with arguments:

interface Test
{
	void fun(int x);
}
class Access 
{
	public static void main(String[] args) 
	{
		Test obj = (x)->{
				System.out.println("Lambda with x value : " + x);
		};
		obj.fun(10);
	}
}

Lambda expression with arguments and with return values:

interface Calculate
{
	int doubleIt(int x);
}
class Access 
{
	public static void main(String[] args) 
	{
		Calculate calc = (x)->{
				return 2*x;
		};
		System.out.println("Double of 4  : " + calc.doubleIt(4));
	}
}
Previous
Next

Add Comment

Courses Enquiry Form