28 Collections Summary - Accessing Elements

As a summary, all collections have a common syntax for accessing elements. Here is a recap

  • You can access the elements of Arrays, Lists Maps and Sets using ()
  • For Maps, accessing a key that doesn’t exist throws an exception: java.util.NoSuchElementException: key not found to check if a key exists use map.contains(key) or use a default value: map.getOrElse(key, defaultValue)

Code editor is using Scalakata.com written by Guillaume Massé