1) Java 8 SAM


2) Use-of-functional-interface-in-java-8


3) Functional-interface-vs-abstract-class


4) Functional-interface-with-no-arguments-and-no-return-value


5) Valid Functional Interface definitions

  • This can be used in lambda expression:
public interface Foo {
  public void doSomething();
  • This cannot be used in lambda expression:
public interface Foo {
  public void doSomething();
  public void doSomethingElse();
  • This will give compilation error:
public interface Foo {
  public void doSomething();
  public void doSomethingElse();

Invalid ‘@FunctionalInterface’ annotation; Foo is not a functional interface


6) Use Bifunction to add two values & print it.

package com.codinko.function;
import java.util.function.BiFunction;
public class BiFunctionDemo {
    public static void main(String[] args) {
        BiFunction<Integer, Integer, String> biFunction = 
                      (num1, num2) -> "Result:" +(num1 + num2);
  • Another use-case of BiFunction.
Map<String,BiFunction<Integer,Integer,Integer>> operators = 
                       new HashMap<>();
operators.put("+", (a,b) -> a + b);
operators.put("-", (a,b) -> a - b);
operators.put("*", (a,b) -> a * b);


7) Difference between BiFunction and BiConsumer?

  • BiFunction accepts two arguments and returns a value.
  • BiConsumer accepts two arguments but does not return value

8) BiConsumer/BiFunction/BiPredicate ?


  • java.util.function.BiPredicate is a functional interface which accepts two argument and returns Boolean value

9) How do you implement an interface that has a run() and throws Exception?

Implement your own!

10) Iterate over values in Hashmap using lambdas , use Consumer or BiConsumer as you feel the need.

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class BiConsumerDemo {
   public static void main(String[] args) {
       Map<Integer,String> map = new HashMap<>();
       map.put(1, "A");
       map.put(2, "B");
       map.put(3, "C");
       BiConsumer<Integer,String> biConsumer = (key,value) -> 
		System.out.println("Key:"+ key+" Value:"+ value);
Output will be as below.
Key:1 Value:A
Key:2 Value:B
Key:3 Value:C