# What exactly is the meaning of "get!(Set{Int}, intersections, highway.nodes\[I\])" in the following code?

**URL:** <https://discourse.julialang.org/t/what-exactly-is-the-meaning-of-get-set-int-intersections-highway-nodes-i-in-the-following-code/27690>\
**Category:** General Usage\
**Created:** [August 18, 2019, 7:35pm UTC](https://discourse.julialang.org/t/what-exactly-is-the-meaning-of-get-set-int-intersections-highway-nodes-i-in-the-following-code/27690 "2019-08-18T19:35:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [August 18, 2019, 7:35pm UTC](https://discourse.julialang.org/t/what-exactly-is-the-meaning-of-get-set-int-intersections-highway-nodes-i-in-the-following-code/27690/1 "2019-08-18T19:35:25Z")

</div>

Hi, I was wondering, could anyone help me with clarifying a little bit of a line of the following code?

```julia
######################################
### Find Intersections of Highways ###
######################################

function find_intersections(highways::Vector{OpenStreetMapX.Way})
    seen = Set{Int}()
    intersections = Dict{Int,Set{Int}}()
    for highway in highways
        for i = 1:length(highway.nodes)
            if i == 1 || i == length(highway.nodes) || (highway.nodes[i] in seen)
                get!(Set{Int}, intersections, highway.nodes[i])
            else
                push!(seen, highway.nodes[i])
            end
        end
    end
    for highway in highways
        for i = 1:length(highway.nodes)
            if i == 1 || i == length(highway.nodes) || haskey(intersections, highway.nodes[i])
                push!(intersections[highway.nodes[i]], highway.id)
            end
        end
    end
    return intersections
end

```

I’m mainly confused with the code `get!(Set{Int}, intersections, highway.nodes[i])`. From my understanding, the first for loop will have the keys of the dictionary ready and the second for loop will have the values of the dictionary ready. However, I’m not exactly sure what is done with the above line of code. Any comments are greatly appreciated.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [August 18, 2019, 10:19pm UTC](https://discourse.julialang.org/t/what-exactly-is-the-meaning-of-get-set-int-intersections-highway-nodes-i-in-the-following-code/27690/2 "2019-08-18T22:19:30Z")

</div>

From `help?> get!`:

> `get!(f::Function, collection, key)`  
> Return the value stored for the given key, or if no mapping for the key is  
> present, store `key => f()`, and return `f()`.

so in this case `Set{Int}` takes the place of `f`, i.e. the function that is called with no arguments to generate default values (as in `Set{Int}()`).

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [August 19, 2019, 8:48am UTC](https://discourse.julialang.org/t/what-exactly-is-the-meaning-of-get-set-int-intersections-highway-nodes-i-in-the-following-code/27690/3 "2019-08-19T08:48:27Z")

</div>

That is also what is in my mind. However, I did not quite see the logic in the above code. From my understanding, the first for loop will have the keys of the dictionary intersections ready. So for each highway, and for each node, if it is already in seen, or if the node index is 1, or if the node index is the number of nodes in highway, then, check to see if `intersections[highway.nodes[i]]` exists or not. If it exists, then simply return it. If not, then assign Set{Int}() to highway.nodes[i]. From my understanding, if it does not exist, then should simply add `highway.nodes[i]` to the dictionary with name intersections.

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [August 19, 2019, 10:33am UTC](https://discourse.julialang.org/t/what-exactly-is-the-meaning-of-get-set-int-intersections-highway-nodes-i-in-the-following-code/27690/4 "2019-08-19T10:33:21Z")

</div>

@ [Bartosz\_Pankratz](https://discourse.julialang.org/u/Bartosz_Pankratz)
