How do I convert a String to Integer using Java 8?

To convert a String to Integer here we use Integer::valueOf method.

  • It is a method reference of Function in java 8.
  • The valueOf(String s, int radix) is an inbuilt method which returns an Integer object.
  • Here we implement convert method of Convertor interface.
  • Now convert method take String parameter and call from con reference which call valueOf method.
  • After converting String to Integer Object and store in Integer value parameter.
  • Now print the value.
				
					@FunctionalInterface
interface Convertor<F, T> {
	T convert(F from);
}

public class TestStream {
	public static void main(String[] args) {
		Convertor<String, Integer> con = Integer::valueOf;
		Integer value = con.convert("123");
		System.out.println(value);
	}
}
				
			

Output :- 123