Apr 4, 2014

Understanding Java 8 Lambda expressions with working examples -- part 1

The function interfaces and Lambda expressions are going to make your code concise when coding with Java 8.  These are the most awaited features among Java developers. If you understand these 2 examples, you will find the other parts easier to understand.

Q. What is a Lambda expression?
A. Lambda expressions are anonymous methods which are intended to replace the bulkiness of anonymous inner classes with a much more compact mechanism. 

Q. What is a functional interface?
AThe annotation @FunctionalInterface is being introduced. This annotation acts similarly to @Override by signaling to the compiler that the interface is intended to be a functional interface. The compiler will then throw an error if the interface has multiple abstract methods.

Example 1:  writing your own functional interface and Lambda expressions

Step 1: Define  a functional interface Summable with one abstract method as it can only have 1 abstract method. You can declare default and static  methods too.

package com.java8.examples;

public interface Summable {
  abstract int sum(int input1, int input2);

Step 2:  Define the SumTest class and provide the Lambda expression to sum 2 numbers. You can think of it as method implementation of  sum(int input1, int input2) defined in the functional interface above.

package com.java8.examples;

public class SumTest  {

 public static void main(String[] args) {
  System.out.println("Sum = " +  new SumTest().sum(2, 3));

 public int sum( int input1,  int input2) {
  //(argument) -> (body) or
  //(input) -> (result)
  Summable adder = (x, y) -> (x + y);

  //invoke anonymous method 
  return adder.sum(input1, input2);

The output will be:

Sum = 5

Example 2: Using the existing Java Comparator interface and implementing your own implementation to compare two numbers.

Step 1: Here is the class that implements the compare(T o1, T o2) via anonymous methods using the Lambda expression.

package com.java8.examples;

import java.util.Comparator;

public class CompareTest {

 public static void main(String[] args) {
  System.out.println(new CompareTest().compare(2, 3));

 public int compare( int input1,  int input2) {
  //(argument) -> (body)
  //(input) -> (result)
  Comparator<Integer> cmp = (x, y) -> {
       return (x < y) ? -1 : ((x > y) ? 1 : 0);
  //invoke anonymous method
  return, input2);

The output:

Step: optional understanding: To understand this better, if you inspect the Comparator interface, it has been enhanced in Java 8 with

  •  @FunctionalInterface annotation
  • a number of default method implementations like reversed( ), thenComparing(Comparator other), etc.
  • a number of static utility methods like naturalOrder ( ), nullsLast(Comparator comparator), etc.

The simplified Comparator functional interface looks like

package java.util;

import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.ToDoubleFunction;
import java.util.Comparators;

public interface Comparator<T> {

     int compare(T o1, T o2);
     boolean equals(Object obj);

  default Comparator<T> reversed() {
       return Collections.reverseOrder(this);
    //...skipping some methods
 public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) {
        return new Comparators.NullComparator<>(false, comparator);


More on Java 8 functional interfaces and Lambda expressions

Labels: ,


Post a Comment

Subscribe to Post Comments [Atom]

<< Home