The get()
method in Julia retrieves the value mapped for a specified key. If there is no mapping for the key, it returns a default value.
get(collection, key, default)
This method takes three arguments.
collection
: This is the collection in which the value is to be retrieved. In this case, the collection is Dictionary
.key
: This is the key whose value is to be retrieved from the Dictionary.default
: This is the default value to be returned if there is no mapping for the provided key.This method returns the value associated with the given key. If no value is associated, it returns the default value.
The code below demonstrates how to use the get()
method.
# Creating a Dictionary SampleDict = Dict("one"=>1, "two"=>2, "three"=>3); ## Getting the element for the key "one" println(get(SampleDict, "one", 1)) ## Getting the element for the key "five" println(get(SampleDict, "five", 5))
Line 2: We create a new Dictionary
named SamplDict
. SamplDict
has three entries: one
, two
, and three
.
Line 5: We use the get()
method to retrieve the value associated with the key one
. The value 1
is returned as the result.
Line 8: We use the get()
method to retrieve the value associated with the key five
. Since there is no entry with key five
, the default value 5
is returned as the result.
RELATED TAGS
CONTRIBUTOR
View all Courses