.get
and then pattern matching over .contains
for a Map SC-R1021Scala's collections return either Some
or None
when retrieving elements depending on whether the requested element exists or not. It is therefore expected that when dealing with such collections, you use .get
and then use pattern-matching over other approaches such as .contains
.
if (map.contains(key)) {
println("key exists")
}
map.get(key) match {
case Some(_) => println("key exists")
case None =>
}