Is there such a thing as bidirectional maps in Scala?

Ivan picture Ivan · Mar 24, 2012 · Viewed 8.7k times · Source

I'd like to link 2 columns of unique identifiers and be able to get a first column value by a second column value as well as a second column value by a first column value. Something like

Map(1 <-> "one", 2 <-> "two", 3 <-> "three")

Is there such a facility in Scala?

Actually I need even more: 3 columns to select any in a triplet by another in a triplet (individual values will never be met more than once in the entire map). But a 2-column bidirectional map can help too.

Answer

Steve picture Steve · Mar 28, 2012

Guava has a bimap that you can use along with

import scala.collection.JavaConversions._