Java 8 — Lambda Expression and Functional Interface | Code Factory

What is Lambda (λ) Expressio? How to write Lambda (λ) Expression?

() -> { System.out.println("Code Factory"); }
// Normal Method :
public void add(int a, int b) {
System.out.println(a + b);
}
//Lambda Expression :
(int a, int b) -> { System.out.println(a + b); }
// Normal Method :
public int getLength(String s) {
return s.length();
}
//Lambda Expression :
(String s) -> { return s.length(); }
// Normal Method :
public void m1() {
System.out.println("Code Factory");
}
//Lambda Expression :
() -> { System.out.println("Code Factory"); }
OR
() -> System.out.println("Code Factory");
// Normal Method :
public void add(int a, int b) {
System.out.println(a + b);
}
//Lambda Expression :
(int a, int b) -> { System.out.println(a + b); }
OR
// Below statement also called Type Inference
(a, b) -> System.out.println(a + b);
// Normal Method :
public int getLength(String s) {
System.out.println(s.length());
}
//Lambda Expression :
(String s) -> { return s.length(); }
OR
(s) -> return s.length();
OR
(s) -> s.length()
OR
s -> s.length()

Characteristics/Properties of Lambda (λ) Expression :

() -> System.out.println("Code Factory");
(s) -> s.length();
(a, b) -> System.out.println(a + b);
(a, b) -> System.out.println(a + b);
(s) -> s.length(); TO s -> s.length();
(int a, int b) -> System.out.println(a + b);
TO
(a, b) -> System.out.println(a + b);
() -> {
stmt1;
stmt2;
}
() -> System.out.println("Code Factory");
s -> s.length();

Functional Interface :

Runnable        ->   contains only run() method
Callable -> contains only call() method
ActionListener -> contains only actionPerformed() method
Comparable -> contains only compareTo() method
// right Functional Interface              ✓
interface Interf {
public void m1();
default void m2() { }
public static void m3() { }
}
// wrong Functional Interface X
interface Interf {
public void m1();
public void m2();
}

@FunctionalInterface :

// valid                            ✓
package com.codeFactory.lambdaExpression;
@FunctionalInterface
interface Interf {
public void m1();
default void m2() { };
public static void m3() { };
}
// not valid                        X
package com.codeFactory.lambdaExpression;
@FunctionalInterface
interface Interf {
public void m1();
public void m2();
}
/*
Unexpected @FunctionalInterface annotation
@FunctionalInterface
^
Interf is not a functional interface
multiple non-overriding abstract methods found in interface Interf
1 error
*/
// not valid                      X
package com.codeFactory.lambdaExpression;
@FunctionalInterface
interface Interf {

}
/*
Unexpected @FunctionalInterface annotation
@FunctionalInterface
^
Interf is not a functional interface
no abstract method found in interface Interf
1 error
*/

Functional Interface with inheritance :

// valid     ✓
package com.codeFactory.lambdaExpression;
@FunctionalInterface
interface InterfP {
public void m1();
}
@FunctionalInterface
interface InterfC extends InterfP {

}
// valid     ✓
package com.codeFactory.lambdaExpression;
@FunctionalInterface
interface InterfP {
public void m1();
}
@FunctionalInterface
interface InterfC extends InterfP {
public void m1();
}
// not valid     X
package com.codeFactory.lambdaExpression;
@FunctionalInterface
interface InterfP {
public void m1();
}
@FunctionalInterface
interface InterfC extends InterfP {
public void m2();
}
/*
Unexpected @FunctionalInterface annotation
@FunctionalInterface
^
InterfC is not a functional interface
multiple non-overriding abstract methods found in interface InterfC
1 error
*/
// valid     ✓
package com.codeFactory.lambdaExpression;
@FunctionalInterface
interface InterfP {
public void m1();
}
interface InterfC extends InterfP {
public void m2();
}

Invoking Lambda Expression By using Functional Interface :

package com.codeFactory.lambdaExpression;interface Interf {
public void m1();
}
class Demo implements Interf {
public void m1() {
System.out.println("m1() method implementation");
}
}
public class Test {
public static void main(String... args) {
Interf i = new Demo();
i.m1();
}
}
package com.codeFactory.lambdaExpression;interface Interf {
public void m1();
}
public class Test {
public static void main(String... args) {
Interf i = () -> System.out.println("m1() method implementation");
i.m1();
}
}
package com.codeFactory.lambdaExpression;interface Interf {
public void add(int a, int b);
}
class Demo implements Interf {
public void add(int a, int b) {
System.out.println(a + b);
}
}
public class Test {
public static void main(String... args) {
Interf i = new Demo();
i.add(10, 20);
i.add(100, 200);
}
}
package com.codeFactory.lambdaExpression;interface Interf {
public void add(int a, int b);
}
public class Test {
public static void main(String... args) {
Interf i = (a, b) -> System.out.println(a + b);;
i.add(10, 20);
i.add(100, 200);
}
}
package com.codeFactory.lambdaExpression;interface Interf {
public int getLength(String s);
}
class Demo implements Interf {
public int getLength(String s) {
return s.length();
}
}
public class Test {
public static void main(String... args) {
Interf i = new Demo();
System.out.println(i.getLength("Code"));
System.out.println(i.getLength("Factory"));
}
}
package com.codeFactory.lambdaExpression;interface Interf {
public int getLength(String s);
}
public class Test {
public static void main(String... args) {
Interf i = (s) -> s.length();
System.out.println(i.getLength("Code"));
System.out.println(i.getLength("Factory"));
}
}
package com.codeFactory.lambdaExpression;class MyRunnable implements Runnable {
@Override
public void run() {
for(int i=0; i<5; i++) {
System.out.println("Child Thred.");
}
}
}
public class Test {
public static void main(String... args) {
Runnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
for(int i=0; i<5; i++) {
System.out.println("Main Thred.");
}
}
}
package com.codeFactory.lambdaExpression;public class Test {
public static void main(String... args) {
Runnable r = () -> {
for(int i=0; i<5; i++) {
System.out.println("Child Thred.");
}
};
Thread t = new Thread(r);
t.start();
for(int i=0; i<5; i++) {
System.out.println("Main Thred.");
}
}
}

Functional Interface Summary :

e.g.  Interf i = () -> System.out.println("Code Factory");
----------- -------------------------------
type λ-Expression

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Spring Boot with TDD(Test Driven Development) — Part I

Adobe XD Installation problem in Creative Cloud

Spring Boot — Exception Handling | Code Factory

On Code Review

Latency, Outliers, and Percentiles

Developer’s Journal #00

Postgres replica conflicts: Part 2

Simple method to Square numbers near to the base

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Responsibility Driven Object Oriented Design

JAVA PLAYLIST.

Comparison Inheritance And Polymorphism in JAVA

Java — Escaping References