Tuesday, March 29, 2016

Map.get - KeyNotFound error

It is interesting how different languages approach towards a given problem.

Let's have a look at how different languages react while trying to read a key from dictionary

Java

map.get('key_that_does_not_exist')

=> null

Ruby

map['key_that_does_not_exist']

=> nil 

Python

>> x['key_that_does_not_exist']
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
KeyError: 'key_that_does_not_exist'

>> x.get('key_that_does_not_exist)
=> None

Scala

val x = Map("a" -> "1”)
x("key_that_does_not_exist”) // Throws exception
x.get("key_that_does_not_exist”) // None