# How to find a key of a Map

**URL:** <https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694>\
**Category:** Question\
**Created:** [February 28, 2020, 1:29pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694 "2020-02-28T13:29:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [February 28, 2020, 1:29pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/1 "2020-02-28T13:29:09Z")

</div>

If I have a `Map[A,Boolean]` and `A` is type for which distinct values might be `==`, is there a way for me to find the key (if any) which is equal to a given object of type `A`?

One application of this would be to _normalize_ a collection so that all `==` objects become `eq`, and thus decrease memory consumption.

---

<div class="post-metadata">

**Author:** ![BalmungSan](https://users.scala-lang.org/user_avatar/users.scala-lang.org/balmungsan/32/1700_2.png) [@BalmungSan](https://users.scala-lang.org/u/BalmungSan)\
**Post date:** [February 28, 2020, 2:37pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/2 "2020-02-28T14:37:41Z")

</div>

I do not understand the question. If two things are the same, then it can’t appear multiple times as the key of a map. And thus, you should only need to `map.contains(x)`.

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [February 28, 2020, 3:27pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/3 "2020-02-28T15:27:34Z")

</div>

Let me give an example.

Suppose I have a `Map[(Int,Int),Boolean]` such and at some point I add `((2,4) -> true)` to it. Later on I create another tuple which happens to have the same components `(2,4)`. If I ask whether `(2,4)` is in the map, the answer is yes. But its not the same `(2,4)` object, it’s just one which happens to contain 2 and 4. How can I ask the map to give me the original `(2,4)` object? I could of course iterate over all the keys, but that would be a linear complexity operation, rather than an amortised constant complexity operation.

A use case. Suppose I have a sequence of 100,000 tuples. I could normalise the 100,000 tuples, but building a Map, and then mapping over the sequence to build a new sequence where each tuple is replaced with the hash key which it is `==` to. This would decrease the memory allocation by 100,000 \* (2 \* 64), i.e., replacing 3 64 bit pointers by 1 such pointer, but repeated 100,000 times, thus saving 16Meg of memory.

---

<div class="post-metadata">

**Author:** ![BalmungSan](https://users.scala-lang.org/user_avatar/users.scala-lang.org/balmungsan/32/1700_2.png) [@BalmungSan](https://users.scala-lang.org/u/BalmungSan)\
**Post date:** [February 28, 2020, 3:41pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/4 "2020-02-28T15:41:01Z")

</div>

Sorry, I still do not get the question.

If you already have an object which is equivalent to the one inside the map, why do you the original one? Unless it is mutable and carries some internal state?

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [February 28, 2020, 3:45pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/5 "2020-02-28T15:45:50Z")

</div>

Because 100,000 copies of an object takes 128 meg of memory, and 100,000 pointers to the same object takes 16 meg of memory. My application deals with exponentially explosive data structures, and I regularly get out-of-memory errors. I’m looking for ways to decrease memory usage.

---

<div class="post-metadata">

**Author:** ![BalmungSan](https://users.scala-lang.org/user_avatar/users.scala-lang.org/balmungsan/32/1700_2.png) [@BalmungSan](https://users.scala-lang.org/u/BalmungSan)\
**Post date:** [February 28, 2020, 3:55pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/6 "2020-02-28T15:55:35Z")

</div>

Yeah I get that, what I still do not understand _(and bear with me that this probably due my bad English)_ is how this relates to the original question.

If I understand you correctly, you want to find if a key exists in the **Map** and retrieve the original key, but what do you want to use to do that check? If you construct an object that is exactly the same one, then how retrieving the original one would help if you already created a copy?

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [February 28, 2020, 4:18pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/7 "2020-02-28T16:18:43Z")

</div>

Here is code which I believe would work, but has quadratic complexity. The function `getKeyFromMap` returns the key from the Map which is equal to its first argument.

```auto
def block[A](body:(A=>Nothing)=>A):A = {
  // CL like block/return, the name of the return() function is provided
  // by the caller.
  // Usage: block{ ret => ... ret(someValue) ...}

  import scala.util.control.NoStackTrace

  class NonLocalExit(val data:A) extends Exception with NoStackTrace {}
  def ret(data:A):Nothing = {
    throw new NonLocalExit(data)
  }
  try{
    body(ret)
  }
  catch{
    case nonLocalExit: NonLocalExit => nonLocalExit.data
  }
}

def getKeyFromMap[T,S](key:T,base:Map[T,S]):Option[T] = {
  block { produce =>
    base.map{ case (k:T, _:S) => if (key == k) produce(Some(k)) }
    produce(None)
  }
}

def normalizeSequence[T,S](seq:Seq[T],base:Map[T,S]):Seq[T] = {
  seq.map{x:T =>
    getKeyFromMap(x,base) match {
      case Some(key) => key
      case _ => x
    }
  }
}

val seq = List((1,2),(2,3),(1,2),(1,2),(2,3))
val base = seq.map{ tuple => (tuple -> true)}.toMap
val normalizedBase = normalizeSequence(seq,base)

// check to confirm that any equal items are actually eq
for { x1 <- normalizedBase
      x2 <- normalizedBase 
      if x1 == x2
      } assert (x1 eq x2)

```

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [February 28, 2020, 4:20pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/8 "2020-02-28T16:20:38Z")

</div>

The way it would help would be that I must have something generating the 100,000 objects, many of which are `==` to each other. So while I’m creating them, I would normalise them, so that the transient values would be garbage which the GC will happily and efficiently collect as ephemeral data. the JVM is very good at reclaiming ephemeral object.

---

<div class="post-metadata">

**Author:** ![shawjef3](https://users.scala-lang.org/user_avatar/users.scala-lang.org/shawjef3/32/486_2.png) [@shawjef3](https://users.scala-lang.org/u/shawjef3)\
**Post date:** [February 28, 2020, 4:45pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/9 "2020-02-28T16:45:38Z")

</div>

@jimka you’re looking for reference equality. Tuples and Produces (case classes are Products) don’t use reference equality for `==`.

```auto
scala> (2,4) eq (2,4)
res1: Boolean = false

scala> (2,4) ne (2,4)
res2: Boolean = true

```

---

<div class="post-metadata">

**Author:** ![jgulotta](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jgulotta/32/1325_2.png) [@jgulotta](https://users.scala-lang.org/u/jgulotta)\
**Post date:** [February 28, 2020, 5:47pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/10 "2020-02-28T17:47:16Z")

</div>

Key retrieval is not part of the current map or set APIs, but if it’s not too much overhead you could use a `TrieMap` (or Java concurrent map) where the value is the same object as the first key that was inserted.

```
@ object Intern {
    private val pool = collection.concurrent.TrieMap.empty[AnyRef, AnyRef]

    def apply[T](value: T): T = pool.getOrElseUpdate(value.asInstanceOf[AnyRef], value.asInstanceOf[AnyRef]).asInstanceOf[T]
  }
defined object Intern

@ val a = (1, 2)
a: (Int, Int) = (1, 2)

@ val b = (1, 2)
b: (Int, Int) = (1, 2)

@ a == b
res12: Boolean = true

@ a eq b
res13: Boolean = false

@ a eq Intern(a)
res14: Boolean = true

@ b eq Intern(b)
res15: Boolean = false

@ a eq Intern(b)
res16: Boolean = true
```

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [February 28, 2020, 6:43pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/11 "2020-02-28T18:43:01Z")

</div>

Is there a weak TrieMap ?

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [February 28, 2020, 7:03pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/12 "2020-02-28T19:03:20Z")

</div>

What are all the @ signs for?

---

<div class="post-metadata">

**Author:** ![jgulotta](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jgulotta/32/1325_2.png) [@jgulotta](https://users.scala-lang.org/u/jgulotta)\
**Post date:** [February 28, 2020, 7:06pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/13 "2020-02-28T19:06:39Z")

</div>

There is no weak TrieMap, but you may be able to use a `WeakReference` to get the behavior you desire.

---

<div class="post-metadata">

**Author:** ![jgulotta](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jgulotta/32/1325_2.png) [@jgulotta](https://users.scala-lang.org/u/jgulotta)\
**Post date:** [February 28, 2020, 7:07pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/14 "2020-02-28T19:07:03Z")

</div>

The `@` is copy/paste from an [ammonite](https://ammonite.io/) REPL session.

---

<div class="post-metadata">

**Author:** ![curoli](https://users.scala-lang.org/user_avatar/users.scala-lang.org/curoli/32/1753_2.png) [@curoli](https://users.scala-lang.org/u/curoli)\
**Post date:** [February 28, 2020, 7:45pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/15 "2020-02-28T19:45:46Z")

</div>

But how are you getting 100,000 independent copies of the same object?

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [February 28, 2020, 8:15pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/16 "2020-02-28T20:15:05Z")

</div>

The 100,000 is a pedagogical example. My data structure uses a weak value hash map to enforce structural identity, ie to assure by construction that all isomorphic objects are eq, making tree equivalence an O(1) operation.

Sadly the Java boss weak value hash map wastes what seems like a huge amount of memory. Therefore I’m investigating whether a weak key hash map might work. But to use a weak keys map I need to find the key given something that hashes to the same thing.

Does it make sense?

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [February 28, 2020, 8:19pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/17 "2020-02-28T20:19:46Z")

</div>

Seems like I may need to learn about implementing weak-references. They are magic to me.

---

<div class="post-metadata">

**Author:** ![eyalroth](https://users.scala-lang.org/user_avatar/users.scala-lang.org/eyalroth/32/896_2.png) [@eyalroth](https://users.scala-lang.org/u/eyalroth)\
**Post date:** [March 1, 2020, 1:06pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/18 "2020-03-01T13:06:37Z")

</div>

> [@jimka](#):
>
> Here is code which I believe would work, but has quadratic complexity. The function `getKeyFromMap` returns the key from the Map which is equal to its first argument.

I’m not exactly sure what is the purpose of this code; is it to demonstrate what you are trying to achieve in general?

In any case, it seems that you’re looking for reference equality (as mentioned by others). According to [this SO question](https://stackoverflow.com/questions/5797373/java-map-based-on-object-reference), you could use Java’s `IdentityHashMap` for this purpose, and potentially [convert it to a Scala immutable map](https://stackoverflow.com/questions/23165925/convert-java-util-identityhashmap-to-scala-immutable-map).

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [March 1, 2020, 1:19pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/19 "2020-03-01T13:19:03Z")

</div>

> [@eyalroth](#):
>
> In any case, it seems that you’re looking for reference equality (as mentioned by others). According to [this SO question](https://stackoverflow.com/questions/5797373/java-map-based-on-object-reference), you could use Java’s `IdentityHashMap` for this purpose, and potentially [convert it to a Scala immutable map](https://stackoverflow.com/questions/23165925/convert-java-util-identityhashmap-to-scala-immutable-map).

Can we make the java IdentityHashMap into a weak-reference hash map, so that objects which are not referenced elsewhere are subject to GC? I’ve never implemented weak-hash-tables but I’ve used them in Lisp. Taking a look at [java-weak-reference](https://www.baeldung.com/java-weak-reference) I admit that I don’t understand java, and I don’t know how to use this information in Scala. Am I supposed to create a `Map[]` in Scala of Scala objects each wrapped by this java object? Will this allow the GC to remove objects from the `Map[]` when they are no longer referenced elsewhere? Or does it man I need to implement some class entirely in java and then `import` it into my Scala program?

---

<div class="post-metadata">

**Author:** ![eyalroth](https://users.scala-lang.org/user_avatar/users.scala-lang.org/eyalroth/32/896_2.png) [@eyalroth](https://users.scala-lang.org/u/eyalroth)\
**Post date:** [March 1, 2020, 1:37pm UTC](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694/20 "2020-03-01T13:37:44Z")

</div>

I am not exactly sure what you are trying to achieve, or what is exactly your use case in which your JVM’s memory explodes.

[Next page](https://users.scala-lang.org/t/how-to-find-a-key-of-a-map/5694.md?page=2)
