Skip to content

Tag: option-type

java 8 Optional with Map<String, Set> [closed]

Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 9 months ago. Improve this question I have a Map<String, Set> getProperties which returns {BUILDING=[a, b, c]}, {NEW_BUIL…

How to get value from an optional object in another optional?

Basically,I need to get a size of optional list in an optional object. Something like: The code doesn’t look nice. What’s the elegant way to get it? With ifPresent().orElse()? Thanks in advance! Answer Consecutive map (or flatMap, in case something returns an Optional) operations, and a final orEl…

How to reduce Optionals to throw an error once

I have the following response coming from a rest call and performing some logic based on what is returned. This is what I want. If the overall status code is NOT 200 OR If within the list of SimpleResponse, none of the SimpleResponse objects has a 200 httpCode, throw an error. The example below is incomplete.…

Set with Optional

I have this code: I want to replace this code to this: and map this field from myClass to myClass2 How i can do it? Answer Did you mean to use: For mapping I would suggest to read about MapStruct, it is a good tool.

Java 8 Optional: combine two possibly null object

I have to ensure if two values are non null. When the first and second have non null values, pass first as argument to second. If one of them are null value, then return false. This can be done in the following piece of code: It can also be done in short form: I was wondering how to do this