Lambda Expression in Java 8 Example

Lambda Expression in Java

In this tutorial, We will show you about, How to create simple Lambda Expression in Java 8. The example program has been tested and shared in the same post.

Functional Interface

package com.dineshkrish;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public interface Arithmetic {

	public int calculate(int a, int b);
}

Implementation Program

package com.dineshkrish;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class AdditionTest {

	public static void main(String[] args) {

		Arithmetic add = (a, b) -> {

			return a + b;
		};

		Arithmetic sub = (a, b) -> {

			return a - b;
		};

		Arithmetic mul = (a, b) -> {

			return a * b;
		};

		Arithmetic div = (a, b) -> {

			return a / b;
		};

		System.out.println(add.calculate(30, 10));
		System.out.println(sub.calculate(30, 10));
		System.out.println(mul.calculate(30, 10));
		System.out.println(div.calculate(30, 10));

	}
}

Output

40
20
300
3

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.