2

I have this map function as part of my stream. parse.apply is basically doing Double::valueOf. My intelliJ suggest me I can replace statement lambda with regular expression Lambda.

.map(x -> { return StringUtils.isEmpty(x) ? parse.apply("0") : parse.apply(x);
   })

If I didn't not have to worry about empty strings I could have done: .map(parse::apply). How do I do in this case?

1
  • What's the "regular expression Lambda"? Commented Jul 29, 2017 at 8:13

1 Answer 1

4

You can filter out all empty strings before reaching .map:

.filter(s -> !StringUtils.isEmpty(s))
.map(parse)

EDIT: if you want to replace empty strings with "0", then simply split one map into two separate operations. It won't affect efficiency and will make your code much easier to understand from functional programming point of view:

.map(s -> StringUtils.isEmpty(s) ? "0": s)
.map(parse)

EDIT2: when you call Double::valueOf you have to be aware that it can throw java.lang.NumberFormatException if given string has no double representation. Be aware of it.

Sign up to request clarification or add additional context in comments.

1 Comment

But I want to replace the empty ones with 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.