Skip to content

Instantly share code, notes, and snippets.

@sdanzig
Last active December 18, 2015 15:09
Using getOrElse to provide a default value when looking up a key in a Map[String,String] ... More details at: http://sdanzig.blogspot.com/2013/06/the-option-design-pattern.html
// Sample usage
Console.println("Last name of harry is: "+
firstToLastNameMap.get("harry").getOrElse("not found"))
Console.println("Last name of harry is: "+
firstToLastNameMap.get("sam").getOrElse("not found"))
/*
Output:
Last name of harry is: smith
Last name of harry is: not found
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment