Yocto vs Ubuntu for Embedded

Yocto Project is a truly great tool to create an embedded Linux distribution of your own. Yes, not just embedded Linux image, but distribution. Now, Raspberry Pi is a great embedded platform for a…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Java Optional

An Optional is really just a list of zero or one items. If Java Optionals were actually defined as such the methods and semantics would make a lot more sense and be more compatible with the rest of the Java library.

Imagine Optional implements List but only allows up to one item. There’s nothing preventing it from doing such. The size would either be 0 or 1. When a user wants to operate on the possibly non-existent element in the Optional, they could call Optional.forEach() or use the stream api. This may seem a little strange at first when I come at it from this angle, but watch this…

What happens when a method returns an Optional and now we want to add that to a collection? The old Optional api would require something wonky like myOptional.ifPresent((value)->myCollection.add(value)). The new api could simply say myCollection.addAll(myOptional). Or even better, myCollection.addAll(myFunction()) where myFunction() is the function that returns the optional. If there are no values in the Optional then a List of zero is returned and zero values are added to the Collection.

The basic idea here is that branching can be eliminated and a more consistent handling of optional return values can be implemented. Also, this new Optional api can fit into the existing stream api more cleanly.

Last, there is no null value in the Optional anymore! The Optional api as it currently exists still contains a null if there is no value and when the user calls Optional.get() a NoSuchElementException is thrown — i.e., just another name for a NullPointerException. With the new api proposed here, there simply is no value. There is no null and no NoSuchElementException! If I’m not mistaken, one of the purposes of Optional was to eliminate NPEs (NullPointerExceptions). It’s hard to do that if null sticks around somewhere. A truly better handling of the absence of a value is to genuinely have the absense of a value. An empty list truly has the absense of a value.

To summarize, Optional as a list of zero or one has the benefit of having true absence of a value, integrates seamlessly with the stream api and Java collections, and eliminates null and the potential for NPEs (by whatever name they go by).

Add a comment

Related posts:

News and journalism charities among winners in Newsquest charity awards

A charitable fund run by Newsquest has confirmed grants of nearly £300,000 to 50 good causes around the UK. Over the last 10 years, the fund has given more than £3 million to help charitable…

Is There A Real Time Forex Rates API For Everyday Use?

Do you want to be up to date with what is happening everyday? If so, a forex rates API is your solution! The foreign exchange market is an over-the-counter (OTC) marketplace that determines the…

Busy Bee

I have always been terrified of bees. The persistent, evil buzzing that ring down your ear canal. The stark contrast of the bright yellow juxtaposed with deep black, shouting at you “Stay Away! I can…