Lambdas

What we’ll cover

What are they?

When to use

Syntax: Runnable, Consumer, Biconsumer, Supplier, Function, Bifunction

What is it?

What is the purpose?

When is this used?

What is a FunctionalInterface?

Syntax

Types of Functional Interfaces

Name
Has First Argument
Has Second Argument Has Return-Type
Runnable
no
no no
Consumer
yes
no
no
BiConsumer
yes
yes
no
Supplier no no yes
Function yes no yes
BiFunction yes yes yes

Runnable

Runnable Syntax
(Lambda)

public class MyClass {
  public static void main(String[] args) {
  	Runnable r = () -> System.out.println("Hello world");
  	r.run(); // invoke the method
  }
}

Runnable Syntax
(Static Method Reference)

public class MyClass {
	public static void main(String[] args) {
		Runnable r = MyClass::myMethod;
		r.run();
	}

	public static void myMethod() {
		System.out.println("Hello world");
	}
}

Runnable Syntax
(Non-Static Method Reference)

public class MyClass {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        Runnable r = obj::myMethod;
        r.run();
    }

    public void myMethod() {
        System.out.println("Hello world");
    }
}

Consumer

Consumer Syntax
(Lambda)

public class MyClass {
  public static void main(String[] args) {
  	Consumer<String> consumer = (str) -> System.out.println(str);
  	consumer.accept("Hello world"); // invoke the method
  }
}

Consumer Syntax
(Static Method Reference)

public class MyClass {
	public static void main(String[] args) {
		Consumer<String> consumer = MyClass::myMethod;
		consumer.accept("Hello world");
	}

	public static void myMethod(String str) {
		System.out.println(str);
	}
}

Consumer Syntax
(Non-Static Method Reference)

public class MyClass {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        Consumer consumer = obj::myMethod;
        consumer.accept("Hello world");
    }

    public void myMethod(String str) {
        System.out.println(str);
    }
}

BiConsumer

BiConsumer Syntax
(Lambda)

public class MyClass {
  public static void main(String[] args) {
    BiConsumer<String, Integer> bc;
    bc = (str, num) -> System.out.println(str + " " + num);
    bc.accept("Hello ", 0);
  }
}

BiConsumer Syntax
(Static Method Reference)

public class MyClass {
  public static void main(String[] args) {
    BiConsumer<String, Integer> bc = MyClass::myMethod;
    bc.accept("Hello", Integer.MAX_VALUE);
  }

  public static void myMethod(String str, Integer num) {
    System.out.println(str + " " + num);
  }
}

BiConsumer Syntax
(Non-Static Method Reference)

public class MyClass {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        BiConsumer<String, Integer> bc = obj::myMethod;
        bc.accept("Hello", Integer.MIN_VALUE);
    }

    public void myMethod(String str, Integer num) {
        System.out.println(str + " " + num);
    }
}

Supplier

Supplier Syntax
(Lambda)

public class MyClass {
  public static void main(String[] args) {
    Supplier<String> supplier = () -> "Hello world";
    String result = supplier.get();
    System.out.println(result);
  }
}

Supplier Syntax
(Static Method Reference)

public class MyClass {
  public static void main(String[] args) {
    Supplier<String> supplier = MyClass::myMethod;
    String result = supplier.get();
    System.out.println(result);
  }

  public static String myMethod() {
    return "Hello world";
  }
}

Supplier Syntax
(Non-Static Method Reference)

public class MyClass {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        Supplier<String> supplier = obj::myMethod;
        String result = supplier.get();
        System.out.println(result);
    }

    public void myMethod() {
        return "Hello world";
    }
}

Function

Function Syntax
(Lambda)

public class MyClass {
  public static void main(String[] args) {
    Function<Date, String> function;
    function = (dateObject) -> String.valueOf(dateObject);
    String result = function.apply(new Date());
    System.out.println(result);
  }
}

Function Syntax
(Static Method Reference)

public class MyClass {
  public static void main(String[] args) {
    Function<Date, String> function = MyClass::myMethod;
    String result = function.apply(new Date());
    System.out.println(result);
  }

  public static String myMethod(Date date) {
    return String.valueOf(date);
  }
}

Function Syntax
(Non-Static Method Reference)

public class MyClass {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        Function<Date, String> function = obj::myMethod;
        String result = function.apply(new Date());
        System.out.println(result);
    }

    public void myMethod(Date date) {
        return String.valueOf(date);
    }
}

BiFunction

BiFunction Syntax
(Lambda)

public class MyClass {
  public static void main(String[] args) {
    BiFunction<Date, Long, String> function;
    function = (date, num) -> String.valueOf(date.getTime() + num);
    String result = function.apply(new Date(), 15L);
    System.out.println(result);
  }
}

BiFunction Syntax
(Static Method Reference)

public class MyClass {
	public static void main(String[] args) {
		BiFunction<Date, Long, String> function = MyClass::myMethod;
		String result = function.apply(new Date(), 180L);
		System.out.println(result);
	}

	public static String myMethod(Date dateObj, Long longObj) {
		return String.valueOf(dateObj + longObj);
	}
}

BiFunction Syntax
(Non-Static Method Reference)

public class MyClass {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        BiFunction<Date, Long, String> function = obj::myMethod;
        String result = function.apply(new Date(), 972L);
        System.out.println(result);
    }

    public void myMethod(Date dateObj, Long longObj) {
        return String.valueOf(dateObj + longObj);
    }
}