useful is in the return position. (…) Simply rename "Optional" to "OptionalResult", or just "Result" and many of the issues go away.” – Stephen Colebourne (2013-06-04 on lambda-dev)
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.” – Brian Goetz (2014-10-12 on SO)
context of a deprecation effort, this seems an entirely reasonable candidate. I'd like to see it fixed, and the sooner the better.” – Brian Goetz (2016-04-26 on core-libs-dev)
Kotlin does it right – see reference for Null Safety var a: String = "abc”; a = null // compilation error var b: String? = "abc”; b = null // ok bob?.department?.head?.name val l = b?.length ?: -1 val l = b!!.length val nullableList: List<Int?> = listOf(1, 2, null, 4) val intList: List<Int> = nullableList.filterNotNull()
“Value type - small immutable, identityless” – John Rose, Brian Goetz, and Guy Steele (State of the Values - April 2014) No support for this yet :( …maybe Java 10.
implements Serializable • Granted, implementing Serializable is hard... “Making something in the JDK serializable makes a dramatic increase in our maintenance costs, because it means that the representation is frozen for all time.” – Brian Goetz (2013-09-28 on jdk8-dev)
sucks.” – Doug Lea • „I call it my billion-dollar mistake.” – Tony Hoare Optional.ofNullable(foo).ifPresent(this::doWork); vs if (foo != null) { doWork(foo); }
should never be forced to use .get() alone • There are other options: • filter() + map() • flatMap() • orElse(V) / orElseGet(Supplier<V> / orElseThrow(Supplier<T>) getMyOptionalResult().get(); vs getMyOptionalResult().orElseThrow(IllegalStateException::new);
There’s more than just .get() and .isPresent()… • …so you should be able to avoid them… • …unless you’re not on Java 9: final Optional<User> userOptional = userRepository.findById(userId); if (userOptional.isPresent()) { doWork(userOptional.get()); } else { LOG.error("User ID {} not found!", userId); }
There’s more than just .get() and .isPresent()… • …so you should be able to avoid them… • …unless you’re not on Java 9: userRepository.findById(userId).ifPresentOrElse( this::doWork, () -> LOG.error("User ID {} not found!", userId));
combining Optionals of Optionals… Optional<BigDecimal> result = first.map(b -> second.map(b::add).orElse(b)) .map(Optional::of) .orElse(second); vs Optional<BigDecimal> result; if (!first.isPresent() && !second.isPresent()) { result = Optional.empty(); } else { result = Optional.of(first.orElse(ZERO).add(second.orElse(ZERO))); }
Optional is a return type • It’s not Serializable by design! • Guava version is, but it was a design mistake „The intent is that the caller immediately check the Optional and extract the actual value if it's present.” – Stuart Marks (2014-07-03 on SO)
to fear Map<Optional<List<Optional<String>>> and the like. The generics type system in Java simply isn't good enough to be doing anything like that, even if it were remotely desirable.” – Stephen Colebourne (2013-06-04 on lambda-dev)
primarily intended for use as a method return type where there is a clear need to represent "no result," and where using null is likely to cause errors. A variable whose type is Optional should never itself be null; it should always point to an Optional instance.” – Optional javadoc
orElse and orElseGet are generally preferable to this method, as they return a substitute value if the value is absent, instead of throwing an exception.” – Optional#get() javadoc
Stuart Marks • “The Design of Optional” by Nicolai Parlog • “Why Isn’t Optional Serializable?” by Nicolai Parlog • ”State of the Values” by John Rose, Brian Goetz, and Guy Steele • JDK mailing lists Resources