Java Stream How to - Create Lambda Function to convert String to Integer








Question

We would like to know how to create Lambda Function to convert String to Integer.

Answer

/* w  ww .j  a v  a 2  s .  c  o m*/
import java.util.function.Function;

public class Main {

  public static void main(String[] args) {
    Function<String,Integer> stringToInt = x -> new Integer(x);

    int i = stringToInt.apply("1");
    System.out.println("String to integer: "+i);
  }

}

The code above generates the following result.