10

I am new to julia!I have just switched from java to julia, Can somebody tell me does julia have hashmap like structures? if No, then how do I map one type to another type in julia?

black sheep 369
  • 564
  • 8
  • 19

2 Answers2

12

Yes!! It does have. Below is how you can create and access one inside Julia.

# Creating the Dict in Julia
julia> hashmap = Dict("language"=>"julia","version"=>"0.6")
        Dict{String,String} with 2 entries:
          "language" => "julia"
          "version"  => "0.6"

# To access individual keys
julia> hashmap["language"]
"julia"

# To find the fields inside a dictionary
julia> fieldnames(hashmap)
8-element Array{Symbol,1}:
 :slots
 :keys
 :vals
 :ndel
 :count
 :age
 :idxfloor
 :maxprobe

# To iterate over the hashmap
julia> for i in hashmap
           println(i)
       end
"language"=>"julia"
"version"=>"0.6"      
Rahul
  • 2,580
  • 1
  • 20
  • 24
6

The Julia Dict is implemented as a hashmap. As with Java, it is important to consider the interface versus the implementation.

Associative is an abstract type that roughly corresponds to Map in Java; these objects can be indexed by their keys to get the corresponding values:

value = associative[key]

Dict is a concrete subtype of Associative that is implemented as an unordered hashmap.

dict = Dict("a" => 1, "b" => 3)
@show dict["a"]  # dict["a"] = 1
Fengyang Wang
  • 11,901
  • 2
  • 38
  • 67