Accessing value of a specified key in Julia - get(), get!() and getkey() Methods - GeeksforGeeks

26 Mar, 2020
The get() is an inbuilt function in julia which is used to return the value stored for the specified key, or the given default value if no mapping for the key is present.
Syntax:get(collection, key, default)
Parameters:
collection: Specified collection.
key: Specified key present in the collection.
default: Specified default value which is returned if no mapping for the key is present in the collection.
Returns: It returns the value stored for the specified key, or the given default value if no mapping for the key is present.
Example:
# Julia program to illustrate # the use of get() method  # Getting the value stored for the# specified key, or the given default# value if no mapping for the key is present.D = Dict("a"=>5, "b"=>10, "c"=>15);println(get(D, "a", 1))println(get(D, "b", 2))println(get(D, "c", 3))println(get(D, "d", 4))
Output:
5
10
15
4

The get!() is an inbuilt function in julia which is used to return the value stored for the specified key, or if no mapping for the key is present, store key => default, and return default.
Syntax:get!(collection, key, default)
Parameters:
collection: Specified collection.
key: Specified key present in the collection.
default: Specified default value.
Returns: It returns the value stored for the specified key, or if no mapping for the key is present, store key => default, and return default.
Example:
# Julia program to illustrate # the use of get !() method  # Getting the value stored for # the specified key, or if no# mapping for the key is present,# store key => default, and return default.D = Dict("a"=>5, "b"=>10, "c"=>15);println(get !(D, "a", 1))println(get !(D, "b", 2))println(get !(D, "c", 3))println(get !(D, "d", 4))println(D)
Output:
5
10
15
4
Dict("c"=>15, "b"=>10, "a"=>5, "d"=>4)

The getkey() is an inbuilt function in julia which is used to return the key matching argument key if one exists in collection, otherwise return default.
Syntax:get!(collection, key, default)
Parameters:
collection: Specified collection.
key: Specified key present in the collection.
default: Specified default value.
Returns: It returns the key matching argument key if one exists in collection, otherwise return default.
Example:
# Julia program to illustrate # the use of getkey() method  # Getting the key matching argument # key if one exists in the collection, # otherwise return default.D = Dict("a"=>5, "b"=>10, "c"=>15);println(getkey(D, "a", 1))println(getkey(D, "b", 6))println(getkey(D, "d", 1))println(getkey(D, "e", 'z'))
Output:
a
b
1
z

Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Get array dimensions and size of a dimension in Julia - size() Method
Searching in Array for a given element in Julia
Find maximum element along with its index in Julia - findmax() Method
Working with Excel Files in Julia
Getting the maximum value from a list in Julia - max() Method
Get number of elements of array in Julia - length() Method
Exception handling in Julia
Working with Date and Time in Julia
Getting the absolute value of a number in Julia - abs() Method
NamedTuple in Julia