Asked  6 Months ago    Answers:  5   Viewed   26 times

Optional type introduced in Java 8 is a new thing for many developers.

Is a getter method returning Optional<Foo> type in place of the classic Foo a good practice? Assume that the value can be null.

 Answers

77

Of course, people will do what they want. But we did have a clear intention when adding this feature, and it was not to be a general purpose Maybe type, as much as many people would have liked us to do so. Our intention was to provide a limited mechanism for library method return types where there needed to be a clear way to represent "no result", and using null for such was overwhelmingly likely to cause errors.

For example, you probably should never use it for something that returns an array of results, or a list of results; instead return an empty array or list. You should almost never use it as a field of something or a method parameter.

I think routinely using it as a return value for getters would definitely be over-use.

There's nothing wrong with Optional that it should be avoided, it's just not what many people wish it were, and accordingly we were fairly concerned about the risk of zealous over-use.

(Public service announcement: NEVER call Optional.get unless you can prove it will never be null; instead use one of the safe methods like orElse or ifPresent. In retrospect, we should have called get something like getOrElseThrowNoSuchElementException or something that made it far clearer that this was a highly dangerous method that undermined the whole purpose of Optional in the first place. Lesson learned. (UPDATE: Java 10 has Optional.orElseThrow(), which is semantically equivalent to get(), but whose name is more appropriate.))

Tuesday, June 1, 2021
 
mozlima
answered 6 Months ago
78

Replace get() with orElse(null).

Thursday, July 15, 2021
 
Zach
answered 5 Months ago
42

There is a typo in your variable path:

C:Program FileJavajdk1.8.0_60bin;C:Program FileJavajdk1.8.0_60bin;

Should be and you do not need it twice

C:Program FilesJavajdk1.8.0_60bin;
Thursday, September 2, 2021
 
Gabriele
answered 3 Months ago
58

You want to use an AnimationTimer or Timeline, not a java.util.Timer. An AnimationTimer or Timeline executes it's code on the JavaFX Application Thread, whereas a java.util.Timer does not. With a java.util.Timer you will encounter random issues related to thread race conditions. You can use Platform.runLater in combination with a java.util.Timer, but, in general, using an AnimationTimer is the preferred way of handling this.

See related question:

  • javafx animation looping
Wednesday, October 20, 2021
 
Kirill Shlenskiy
answered 1 Month ago
42

First, check if the Optional is present. If yes, then stream the list and filter the non-empty ones and print each of them.

optionalList.ifPresent(list -> list.stream()
            .filter(Optional::isPresent)
            .map(Optional::get)
            .forEach(System.out::println));

Almost similar for the stream case too

optionalStream.ifPresent(stream -> stream
            .filter(Optional::isPresent)
            .map(Optional::get)
            .forEach(System.out::println));
Wednesday, November 3, 2021
 
Ergwun
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share