Skip to content
Advertisement

Tag: option-type

How to properly unwrap an optional in Java?

I am learning the basics of Java and I am exploring Optionals and abstract classes so I came across the following issue, I have this code I was expecting to see the prints on the console “It is a Dog” followed by 2 “It is not a Dog” Since I’m using the method .isPresent() on optionals, But I got 1

Java records with nullable components

I really like the addition of records in Java 14, at least as a preview feature, as it helps to reduce my need to use lombok for simple, immutable “data holders”. But I’m having an issue with the implementation of nullable components. I’m trying to avoid returning null in my codebase to indicate that a value might not be present.

Using Java Optional in Kotlin DTOs

Currently I’m using Java 8’s Optional in my Kotlin DTOs as follows: The purpose of it is that for some properties like prop2 I want to also allow delete in the request. So the value of prop2 in the DTO is understood as follows: null => do nothing non-empty optional => update value empty optional => delete value (set value

MapStruct – Can’t map property Optional to LocalDate

NOTE: This is unlike other questions on StackOverflow because they resolve this issue by mapping the two classes manually. Since ScheduleSource and ScheduleTarget are exactly the same classes, I want them to be mapped automatically. Hi, I have 2 classes ScheduleSource and ScheduleTarget. They have exactly the same properties. When I try to use MapStruct to map from ScheduleSource to

How to implement the Elvis operator in Java 8?

I have the classic “Elvis operator” case, where I’m calling methods that each may return null and chaining them together: In Java 8, the most faithful implementation I’ve found is something like this: I wish that Java’s Optional had something akin to the elvis operator: So that I wouldn’t have to wrap each return value: Is there a more efficient

How to map an OptionalLong to an Optional?

I have an instance of OptionalLong. But one of my libraries requires an Optional<Long> as a parameter. How can I convert my OptionalLong into an Optional<Long>? I was dreaming about something like this: Answer I don’t know simpler solutions but this will do what you need.

How to peek on an Optional?

I want to use the fluent api of Optional and apply two Consumers to it. I’m dreaming about something like this: How do I apply several Consumers to an Optional? Answer You can use this syntax:

How to return when an optional is empty?

I love that optionals are in the Java standard library now. But there is one basic problem that I keep running into that I haven’t figured out how to solve in the best way (easiest to read and understand, prettiest, shortest): How to return from a method when an optional is empty? I am looking for a general solution that

Difference between Java Optional and Scala Option

At the very end, this article introducing to new Java 8 Optional, states that Optional is not nearly as powerful as Option[T] in Scala (but at least it doesn’t allow wrapping null). The API is not as straightforward as null-handling and probably much slower. But the benefit of compile-time checking plus readability and documentation value of Optional used consistently greatly

Advertisement