Justin A Justin A - 1 year ago 72
Java Question

How to prevent java stream from running the same method twice?

Im trying to build a map of names to a values. I have this stream that is calling the method getValue twice. Once to make sure the return value isn't null and then again to actually collect the value. I'm trying to figure out how to add a lambda block to capture the return object.

enum -> enum.name(),
enum -> ((issue.getValue(enum) != null) ? issue.getValue(enum) : "")

I keep getting various IDE syntax errors as i try different arrangements. I feel like this should be trivial but yet it is eluding me. Any help would be awesome...

Answer Source

Just use a block instead of a single expression:

theEnum -> { String v = issue.getValue(theEnum); return v != null ? v : ""; }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download