Java 8 Optional Example - isPresent(), OrElse() and get()

The Optional class in Java is one of many goodies we have got from the Java 8 release. If you use correctly, Optional can result in clean code and can also help you to avoid NullPointerException which has bothered Java developer from its inception. Even though many of us have used null to indicate the absence of something, the big problem is that if you call a method or access a field on the null object (except static fields), you will get a NullPointerException and your whole program may crash. The bigger problem is to find the faulty code or root cause because NullPointerException only indicates the line when you try to access field or variable from a null object but how does that null is get created on the code is unknown.

I have seen many developer spending nights finding the source of dreaded NullPointerException. Thankfully some of those problems can be resolved by correctly using Optional class.

The biggest benefit of using Optional class is that it improves the readability of code. Now, you don't need to guess whether a value is present or not. Optional itself indicate that a value may be present.

You just need to take the literal meaning of Optional that value may or may not be present and you have to code accordingly. This will think you what to do in case of value is not present like using a default value or throwing a proper error message.

On a related note,  Java is moving really fast and we are already in Java 12, still, a lot of developers has to learn Java 8, particularly the functional programming aspect. If you think that your Java 8 skills are not at par or you want to improve yourself, I suggest you join a comprehensive Java course like The Complete Java MasterClass. It covers everything you need to know and also recently updated for Java 11.




How to use Optional in Java 8

Optional is nothing but a container, you can also view it as a Stream of one or none values. Now, let's see some examples of Optional class to write clean code in Java.

Suppose, we need to write a method which takes a letter and return the first book starting with that letter in the library. If there is no book found for that letter we return null.

package tool;

import java.util.Arrays;
import java.util.List;

/**
 * Program to demonstrate how to use Optional in Java 8 
 * 
 * @author WINDOWS 8
 *
 */
public class Hello {
  private static List<String> listOfBooks = Arrays.asList("Effective Java",
      "Clean Code", "Test Driven");

  /*
   * Return the first book start with a letter.
   */
  public static String getBook(String letter) {
    String found = null;
    for (String book : listOfBooks) {
      if (book.startsWith(letter)) {
        found = book;
        break;
      }
    }

    return found != null ? found : "Book not Found";
  }
}



You can replace the null check in this method using Optional.get() and Optional.isPresent() method and imperative code with the new Java 8 functional code using Stream API.

If you are not familiar with Stream API  then check the Javadoc of Java SE 8 or higher version, and if you find Javadoc boring then you can also check out From Collections to Streams in Java 8 Using Lambda Expressions course on Pluralsight. It's one of the best course on Collection and Stream and I highly recommend all Java developers to take a look at it.

Java 8 Optional Example - isPresent(), OrElse() and get()


It's an excellent course but you would need a Plurlasight membership to access this course, which cost around $29 per month or $299 per year. This may seem a bit expensive at first but its well worth because it provides access to more than 5000 online courses on Pluralsight which you can use to learn any latest technology.

I have bought Pluralsight membership and I also suggest you join it if you can invest that much money in a year for your learning, and they are also offering $100 off now, just check.

Anyway, even if you don't have Plurlasight membership, you can still access this course for free by signing up for the 10-day Free Pass which provides 200 minutes of access to all of their courses.




Modified Java Program using Optional Object

Anyway here is our modified Java program using the Optional feature of Java 8:

package tool;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * Program to demonstrate how to use Optional in Java 8
 * 
 * @author WINDOWS 8
 *
 */
public class Hello {
  private static List<String> listOfBooks = Arrays.asList("Effective Java",
      "Clean Code", "Test Driven");

  /*
   * Return the first book start with a letter.
   */
  public static String getBook(String letter) {
    Optional<String> book 
          = listOfBooks.stream()
                       .filter(b -> b.startsWith(letter))
                       .findFirst();

    return book.isPresent() ? book.get() : "Book Not Found";
  }
}


What we have done here is instead of using String to represent a book, we have used Optional<String> which means the variable may or may not contain a book.

We have also used the new Stream API to convert our imperative style code to new  Java 8 functional style. Since Stream API returns Optional it makes it easy to explain the example.

Anyway, if you look closely, the new check is not better than the old null check. If you compare, the old one was even shorter and cleaner. So, we are not really using Optional in the right way.

That's actually the main problem for Java developers who have not understood the Optional concept fully. Instead of using ifPresent() and get(), it's better to use OrElse() method which can return a default value if Optional doesn't contain a value. Using plain get() is not an option because it either return value or null.

Anyway, by using orElse() method we can rewrite the above code as shown below, which is much more cleaner and expressive.

/*
   * Return the first book start with a letter.
   */
  public static String getBook(String letter) {
    Optional<String> book 
          = listOfBooks.stream()
                       .filter(b -> b.startsWith(letter))
                       .findFirst();

    return book.orElse("Book Not Found");
  }
You can see that OrElse() method is much nicer than the combination of isPresent() and get(). It not only removes the check but also more expressive like either return a book or else a specified String.

The Optional API is also rich and provide a couple of useful methods like ifPresent() and orElseThrow() which can be used to throw an Exception if the value is not present. You can further see The Complete Java MasterClass on Udemy to learn about Java API and the modern way of Java Coding.

Java 8 Optional Example




Java Program Using Optional Feature of Java 8

And, here is the screenshot of a complete program to use Optional in Java 8  for your reference:


Java 8 Optional Example - isPresent(), OrElse() and get()




That's all about how to use the Optional class in Java 8. Even though it's a poor cousin of Scala's Option type and Haskel's Maybe type, you can still write cleaner code and avoid NullPointerException if used correctly. Don't just use it blindly otherwise you would end up with poor code, sometimes worse than plain null checks.

Further Learning
The Complete Java MasterClass
Java 8 New Features In Simple Way
From Collections to Streams in Java 8 Using Lambda Expressions
10 Examples of Optional in Java 8


Related Java 8 Tutorials
If you are interested in learning more about new features of Java 8, here are my earlier articles covering some of the important concepts of Java 8:
  • How to join String in Java 8 (example)
  • How to use filter() method in Java 8 (tutorial)
  • Java 8 - Stream.collect() Example (example)
  • How to use Stream class in Java 8 (tutorial)
  • How to use forEach() method in Java 8 (example)
  • What is double colon operator of Java 8? (tutorial)
  • 20 Examples of Date and Time in Java 8 (tutorial)
  • 5 Books to Learn Java 8 from Scratch (books)
  • How to use peek() method in Java 8 (example)
  • Java 8 Stream.findFirst() + filter() example (see)
  • How to convert List to Map in Java 8 (solution)
  • How to join String in Java 8 (example)
  • Difference between abstract class and interface in Java 8? (answer)
  • 10 Free Courses for Experienced Java Programmers (courses)
  • How to use peek() method in Java 8 (example)
  • How to sort the may by values in Java 8? (example)
  • How to format/parse the date with LocalDateTime in Java 8? (tutorial)
  • 5 Free Courses to learn Java 8 and 9 (courses)

Thanks for reading this article so far. If you like this tutorial then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

P.S.: If you want to learn more about new features in Java 8 then please see the tutorial What's New in Java 8. It explains all the important features of Java 8 like lambda expressions, streams, functional interface, Optional, new Date Time API and other miscellaneous changes.

No comments:

Post a Comment