BiDirectionalMap

Table of contents
  1. Use case
  2. Creation
  3. Modification
  4. Lookup elements

Use case

BiDirectionalMaps can be used to model relationships where every key has exactly one value and vice versa. It may for example be used to map domain model objects to the corresponding view /components. The map enables mapping in both directions.

Creation

The map takes two generic parameters for the type of the domain and co-domain objects. The constructor takes initial elements as varargs.

val map: Bidirectionalmap<Int, String> = Bidirectionalmap(
  Tuple<Int, String>(1, "ONE"),
  Tuple<Int, String>(6, "SIX"),
  Tuple<Int, String>(10, "TEN")
)

Note that an IllegalArgumentException is thrown if the parameters contain duplicate entries in the domain or co-domain.

Modification

Adding and removing elements from the map can be accomplished by add and remove. Adding elements with an already contained key or value will result in an unchanged map and return value false.

If an element is already contained can be checked with the following methods:

Lookup elements

Looking up elements can be accomplished by

The set of entries gets returned by