Skip to content

Instantly share code, notes, and snippets.

@devth
Last active August 29, 2015 14:12
Show Gist options
  • Save devth/e21aaccefe2b7c277ded to your computer and use it in GitHub Desktop.
Save devth/e21aaccefe2b7c277ded to your computer and use it in GitHub Desktop.
def getOrCreate[A](m: JMap[Any, Any], key: Any, create: A): A = {
if (m.containsKey(key)) m.get(key).asInstanceOf[A]
else {
m.put(key, create)
create
}
}
def storeAtPath(m: JMap[Any, Any], path: Seq[String], values: Seq[Any]) {
path.toList match {
case key :: Nil => {
// Insert the value
m.put(key, values)
}
case key :: rest => {
// Create another submap
val subMap = getOrCreate(m, key, new JHashMap[Any, Any])
storeAtPath(subMap, rest, values)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment