Publish Your Article Here

Lambda example with single method interface implementation.

Functional interfaces are an interface with single abstract method. Lambdas are type of functional interfaces. Java API has many single method implementations like Comparator, Runnable, Callable, etc. Prior to Java-8, you can implement and initiaize them using anynomus class syntax.

In this page we have given a simple example on Lambda implementation on single method interface. We have taken Runnable interface for the example. It has only one method called "run". In the example notice that how lambda reduced multiple lines of code to single line:

package com.java2novice.lambda;

public class FuncIntLambdaEx {

	public static void main(String a[]) {

		// normal way of anonymous class implementation
		Runnable rnnObj = new Runnable() {

			public void run() {
				System.out.println("Runnable interface with normal anonymous class implementation...");

		// anonymous class implementation with Lambda
		Runnable rnnObjLImpl = () -> System.out.println("Runnable interface with Lambda implementation...");;;

Runnable interface with normal anonymous class implementation...
Runnable interface with Lambda implementation...
<< Previous Program | Next Program >>

Java-8 Lambda expression examples

  1. Lambda expression syntax example
  2. Lambda example with single method interface implementation.
  3. Comparator example with Lambda implementation.
  4. How to return lambda as an object?
Knowledge Centre
ServletOuptputStream Vs PrintWriter
ServletOutputStream: ServletResponse.getOutputStream() returns a ServletOutputStream suitable for writing binary data in the response. The servlet container does not encode the binary data, it sends the raw data as it is.

PrintWriter: ServletResponse.getWriter() returns PrintWriter object which sends character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified then it does default character encoding.
Famous Quotations
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.