Of course, migrating to a modern release of Java such as Java SE 8 is a momentous task. Java 8 is a revolutionary release with a huge upgrade to the model and a coordinated evolution of JVM, Java language and libraries. However, there are some best practice measures with Java 8 code that are useful in general whether you are considering migration or building a new. Here are a few of them:

Optional

Optional is quite underappreciated as a feature. It has the capability to remove quite a lot of the NullPointerExceptions that are the bane of Java developers. Optional is specifically important at the boundaries of code (for APIs you might be using or exposing) as it allows you and the calling code to reason what to expect.

However, optional should be used with careful thought and design as it can impact many classes. Optional should only be used for return types and not parameters, neither fields. Luckily, IntelliJ IDEA allows you to inspect if you are following the recommendations for optional use.

private Optional<T> convertedValue;

public final Object encode(final Optional<T> value) {

return encode(value = null);

}

IntelliJ IDEA’s recommendations will prevent Optional leaking all over your code, so remember you should deal with Optional where you found it and move swiftly on.

final Optional<Key> key = mapper.getConverters().decode(keyClass, dbObject, mappedField);

if (key.isPresent() ) {

proxiedMap.put(key.get(), value);

}