0

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
Caner
  • 57,267
  • 35
  • 174
  • 180
  • @Oleksandr its not duplicate read carefully – Caner Aug 14 '18 at 10:05
  • Nitpick, but a String value can't be null. A String _variable_ can. – Amadan Aug 14 '18 at 10:06
  • 1
    I agree with @Oleksandr there is an answer there that is a duplicate of this – Eugene Aug 14 '18 at 10:12
  • 1
    An answer being a duplicate does not mean the question is a duplicate. [Handling duplicate questions](https://stackoverflow.blog/2009/04/29/handling-duplicate-questions/) (EDIT: Not saying the question is or isn't, just as a comment so we're all on the same page, whether _an answer_ is a duplicate is not a closure criterion.) – Amadan Aug 14 '18 at 10:14
  • @Amadan agreed, but even the question is a duplicate – Eugene Aug 14 '18 at 10:15
  • @Eugene question is not duplicate. Other one asks for int not Integer, and related to Java and not Java 8+, and don't have the same constraints(single line, no if else) – Caner Aug 14 '18 at 10:15
  • 2
    @Caner of course in your opinion it's not a duplicate, my impression is that it is - as such I voted to close it. – Eugene Aug 14 '18 at 10:18
  • @Caner you need to edit the question to make it clear why you think it's not a duplicate. From my perspective `int` and `Integer` are the same thing. – ChrisF Aug 14 '18 at 10:59
  • I think your question might be suitable for reopening if you clearly specify the problem that `x` might be `null`, instead of requiring people to infer this from the comment _"// NullPointerException"_. But in its current form, it is a duplicate in my opinion (even ignoring the fact that even if you address this, the code will still fail when `x` is not a valid numerical string (and thus throws a `NumberFormatException`). – Mark Rotteveel Aug 14 '18 at 13:00

2 Answers2

5
int value = Optional.ofNullable(x).map(Integer::parseInt).orElse(0);

This will result in a default value of 0 if the input String is null.

As an alternative, use:

Integer value = Optional.ofNullable(x).map(Integer::valueOf).orElse(null);

which will result in null if the input String is null.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

What about using ? operator instead of one line if...else?

Integer value = x != null ? Integer.valueOf(x) : null;
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • this is still a hidden if/else – Eugene Aug 14 '18 at 10:08
  • thats a bit cheating because x ? a : b is basically an if – Caner Aug 14 '18 at 10:09
  • Yes, but I found this is more simple than `Stream's Optional`. Anyway, you could choose the best solution for your particular problem. – Oleg Cherednik Aug 14 '18 at 10:09
  • Depends how you look at it. If your problem is "I need to calculate this in an expression context, so I can't use any statements, like `if`, then this is a viable answer. However, anyone who knows about Optional monads will likely know `?:` is the expression equivalent of the `if` statement, and wouldn't post a question if it was about that in the first place. – Amadan Aug 14 '18 at 10:11
  • I agree this looks more simple – Caner Aug 14 '18 at 10:11