# Getkey for dictionaries with composite-typed values

**URL:** <https://discourse.julialang.org/t/getkey-for-dictionaries-with-composite-typed-values/83940>\
**Category:** General Usage\
**Tags:** question, dictionary\
**Created:** [July 8, 2022, 10:11am UTC](https://discourse.julialang.org/t/getkey-for-dictionaries-with-composite-typed-values/83940 "2022-07-08T10:11:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![taxodium](https://avatars.discourse-cdn.com/v4/letter/t/ecae2f/32.png) [@taxodium](https://discourse.julialang.org/u/taxodium)\
**Post date:** [July 8, 2022, 10:11am UTC](https://discourse.julialang.org/t/getkey-for-dictionaries-with-composite-typed-values/83940/1 "2022-07-08T10:11:24Z")

</div>

Hi.  
I’m intermediate level in Julia.

For some problem I want to solve, I have to define a custom composite type:

```julia
struct NodeData
    presence_cpu::AbstractVector{Bool}
    presence_gpu::AbstractVector{Bool}
    mem_allocation_cpu::Float64
    mem_allocation_gpu::Float64
    state::Int64
end

```

and I then create a dictionary with values of this custom type with `dict_nodes = Dict{Int64,NodeData}()`

As advised by the docs I created my own hash function for `NodeData`, which allows me to check is some `NodeData` element is in the dictionary.

But for some reason I need to get the key corresponding to a specific value of the dictionary.  
I’m confused because it doesn’t work as shown by this minimal example:

```julia
dict_nodes
# returns:
Dict{Int64, NodeData} with 1 entry:
  0 => NodeData(Bool[1, 0, 0, 1, 0], Bool[0, 0, 0, 0, 0], 1100.0, 0.0, 0)

# then
el0 = NodeData(Bool[1, 0, 0, 1, 0], Bool[0, 0, 0, 0, 0], 1100.0, 0.0, 0)
el0 in values(dict_nodes)
# returns
true

# but the command
getkey(dict_nodes, el0, nothing)
# returns nothing

```

I couldn’t find the solution to this anywhere.  
Thanks in advance for some suggestions or help.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [July 8, 2022, 11:05am UTC](https://discourse.julialang.org/t/getkey-for-dictionaries-with-composite-typed-values/83940/2 "2022-07-08T11:05:53Z")

</div>

From Julia docs: `getkey(collection, key, default)` Return the key matching argument `key` if one exists in `collection` , otherwise return `default`.

The second argument of `getkey` is a key (for example, 0 in your `dict_nodes`), not a value. Also, you’re required to define `hash()` and `isequal()` for keys, not values. In your case the keys are integers not custom structs, so there’s nothing required from you.
