Optional

While working on exercises contained in the Object-oriented programming MOOC from the University of Helsinki, I found myself trying to get a non-existent key from a HashMap.

This returned a NullPointerException. I thought this would be a sensible situtation to work with the Optional API.

Scenario

Suppose we have the following usernames and passwords stored in a HashMap:

Username Password
alex mightyducks
emily cat
HashMap<String, String> credMap = new HashMap<>();

credMap.put("alex", "mightyducks");
credMap.put("emily", "cat");

We have written a program that prompts for username and password:

System.out.println("Type your username:");
String username = reader.nextLine();

System.out.printf("Type your password:");
String password = reader.nextLine();

If we type in the correct credentials then good. And if we type in the incorrect credentials for alex or emily, then we can handle that too.

if(credMap.get(username).equals(password))
System.out.println("You are now logged into the system!");
else
System.out.println("Your username or password was invalid!");     

But what if we type in, say, John– a nonexistent username? The HashMap will return a NULL which will cause Java to error as soon as we attempt to invoke equals().

We can simply wrap the HashMap in an Optional. Now:

Optional<String> cred = Optional.ofNullable(credMap.get(username));

if(cred.isEmpty())
    System.out.println("Your username or password was invalid!");
else if(cred.get().equals(password))
    System.out.println("You are now logged into the system!");