Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 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

get!()

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)

getkey()

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :