Skip to the content.

Lambda Expressions

-

What we’ll cover

-

Functional Interfaces

-

What is a Functional Interface?

-

Relevant Jargon

-

Functional Interfaces

| Name | Has Return-Value | Takes Argument 1 | Takes Argument 2 | |————|———|——————|——————| | Runnable | No | No | No | | Supplier | Yes | No | No | | Consumer | No | Yes | No | | BiConsumer | No | Yes | Yes | | Function | Yes | Yes | No | | BiFunction | Yes | Yes | Yes |

-

Functional Interfaces

-

Functional Interface

public class MainClass {
  public void demo() {
    Runnable runnable = this::someRunnableMethod;
    runnable.run(); // invokes method
  }

  public void someRunnableMethod() {
      System.out.println(new StringBuilder()
              .append("I don't take any arguments and ")
              .append("I don't return anything!")
              .toString());
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    Runnable runnable = MainClass::someRunnableMethod;
    runnable.run(); // invokes method
  }

  public static void someRunnableMethod() {
      System.out.println(new StringBuilder()
              .append("I don't take any arguments and ")
              .append("I don't return anything!")
              .toString());
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    Runnable runnable = () -> {
      System.out.println(new StringBuilder()
              .append("I don't take any arguments and ")
              .append("I don't return anything!")
              .toString());
    }
    runnable.run(); // invoke method
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    Supplier<String> supplier = this::someSupplierMethod;
    String result = supplier.get();
  }

  public String someSupplierMethod() {
      return new StringBuilder()
              .append("I don't take any arguments and ")
              .append("I return a String!")
              .toString();
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    Supplier<String> supplier = MainClass::someSupplierMethod;
    String result = supplier.get();
  }

  public static String someSupplierMethod() {
      return new StringBuilder()
              .append("I don't take any arguments and ")
              .append("I return a String!")
              .toString();
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    Supplier<String> supplier = () -> new StringBuilder()
            .append("I don't take any arguments and ")
            .append("I return a String!")
            .toString();
    String result = supplier.get();
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    Consumer<String> consumer = this::someConsumerMethod;
	 consumer.accept("Hello world");
  }

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

-

Functional Interface

public class MainClass {
  public void demo() {
    Consumer<String> consumer = MainClass::someConsumerMethod;
    consumer.accept("Hello world");
  }

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

-

Functional Interface

public class MainClass {
  public void demo() {
    Consumer<String> consumer = (str) -> System.out.println(str);
    consumer.accept("Hello world");
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    BiConsumer<String, String> consumer = this::someConsumerMethod;
    consumer.accept("Hello", "World");
  }

  public void someConsumerMethod(String str1, String str2) {
    System.out.println(str1 + str2);
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    BiConsumer<String, String> consumer = MainClass::someConsumerMethod;
    consumer.accept("Hello", "World");
  }

  public static void someConsumerMethod(String str1, String str2) {
    System.out.println(str1 + str2);
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    BiConsumer<String, String> consumer
      = (str1, str2) -> System.out.println(str1 + str2);
    consumer.accept("Hello", "World");
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    Function<Integer, String> function = this::method;
    String result = function.apply(10);
  }

  public String method(Integer val1) {
    return String.valueOf(val1);
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    Function<Integer, String> function = MainClass::method;
    String result = function.apply(10);
  }

  public static String method(Integer val1) {
    return String.valueOf(val1);
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    Function<Integer, String> function = (intVal) -> String.valueOf(intVal);
    String result = function.apply(10);
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    BiFunction<Integer, Long, String> function = this::method;
    String result = function.apply(10, 25000);
  }

  public String method(Integer val1, Long val2) {
    return String.valueOf(val1 + val2);
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    BiFunction<Integer, Long, String> function = MainClass::method;
    String result = function.apply(10, 25000);
  }

  public static String method(Integer val1, Long val2) {
    return String.valueOf(val1 + val2);
  }
}

-

Functional Interface

public class MainClass {
  public void demo() {
    BiFunction<Integer, Long, String> function =
      (val1, val2) -> String.valueOf(val1 + val2);
    String result = function.apply(10, 25000);
  }
}

-