Java Lambda Expression Basics

Java 8 introduced Lambda expressions. Lambda expressions are a shorthand for implementing single method interfaces. By a single method interface, I mean something like the following.

package com.tgenedavis.lambda;

public interface Flower {
	public void bloom();
}

The interface Flower has a single method called bloom(). Before Lambda expressions, we could implement single method interfaces with an anonymous class or with a normal class.

(For a quick overview of Functional Programming, I have a Functional Programming Primer.)

Here is an example of implementing a single method interface named Flower with a traditional class.

package com.tgenedavis.lambda;

public class Rose implements Flower {

	@Override
	public void bloom() {
		System.out.println( "I'm a bloomin' flower." );
	}

}

Here’s an example of implementing a single method interface named Flower with an anonymous class.

package com.tgenedavis.lambda;

public class GreenThumb {

	public static void main(String[] args) {

		GreenThumb gt = new GreenThumb();
		gt.growFlowers(new Flower() {

			@Override
			public void bloom() {
				System.out.println( "I'm a pretty blooming flower." );
			}

		});

	}

	public void growFlowers( Flower f ) {
		f.bloom();
	}

}

Whether you use an anonymous class, or a full blown class to implement the single method interface, there’s a lot of work do before you even get around to writing the code that goes in the single method you want implemented.

Lambda expressions remove all this extra work.

First we make a small change to the original interface definition. We add the @FunctionalInterface annotation to the interface’s method. The Flower interface changes to look like the following code.

package com.tgenedavis.lambda;

@FunctionalInterface
public interface Flower {
	public void bloom();
}

Now we modify the anonymous class to look like the following.

package com.tgenedavis.lambda;

public class GreenThumb {

   public static void main(String[] args) {

      GreenThumb gt = new GreenThumb();
      gt.growFlowers(
            () -> System.out.println("I'm a pretty blooming flower.")
            );
   }

   public void growFlowers(Flower f) {
      f.bloom();
   }

}

Notice that the anonymous class went from,

new Flower() {

   @Override
   public void bloom() {
      System.out.println( "I'm a pretty blooming flower." );
   }
}

to a Lambda expression that looks like the following.

() -> System.out.println("I'm a pretty blooming flower.")

Why yes, … this is the coolest thing since sliced bread.

Using a Lambda expression, you don’t have to tell the Java compiler that you are implementing a Flower, or that you are implementing the bloom() method. The compiler is smart enough to know the class and the method you are implementing without the added hassle of telling it.

Now for the variations.

If you are passing parameters into the implemented method, place those arguments in the parens of the Lambda expression. For example,

(x, y) -> System.out.println("x = " + x + " y = " + y)

You can specify the argument types for clarity.

(int x, int y) -> System.out.println("x = " + x + " y = " + y)

You can implement multi-line methods using curly braces. Don’t forget your semicolons for line endings in code blocks.

(int x, int y) -> {
   System.out.println("x = " + x);
   System.out.println("y = " + y);
}

If the method you are implementing has only one parameter of an obvious type, you can ignore the initial parens, like the following example.

x -> System.out.println("x = " + x)

That’s all the basics of Java 8’s Lambda expressions. They have a lot of power besides just replacing anonymous classes, but that’s the subject of a more advanced tutorial.