Java Stream How to - Assign method from Java buildin class to a functional interface








Question

We would like to know how to assign method from Java buildin class to a functional interface.

Answer

/*  w  w w. j a  va 2  s  .  c  o m*/

public class Main {
  public static void main(String... args) {
    Converter<String, Integer> integerConverter2 = Integer::valueOf;
    Integer converted2 = integerConverter2.convert("123");
    System.out.println(converted2);   // result: 123

  }
}

@FunctionalInterface
interface Converter<F, T> {
    T convert(F from);
}

The code above generates the following result.