This sounds simple, but I can't find a way to convert a String
value(possibly null) to Integer
in single line without using if else
in Java 8+. The answer might involve usage of ofNullable
and isPresent
.
Some things I have tried:
String x = ...;
Integer.valueOf(x); // fails if x is null
Optional.ofNullable(Integer.valueOf(x)).orElse(null); // NullPointerException