Red-Black tree-based implementation of BidiMap where all objects added
This class guarantees that the map will be in both ascending key order
and ascending value order, sorted according to the natural order for
the key's and value's classes.
This Map is intended for applications that need to be able to look
up a key-value pairing by either key or value, and need to do so
with equal efficiency.
While that goal could be accomplished by taking a pair of TreeMaps
and redirecting requests to the appropriate TreeMap (e.g.,
containsKey would be directed to the TreeMap that maps values to
keys, containsValue would be directed to the TreeMap that maps keys
to values), there are problems with that implementation.
If the data contained in the TreeMaps is large, the cost of redundant
storage becomes significant. The
DualHashBidiMap implementations use this approach.
This solution keeps minimizes the data storage by holding data only once.
The red-black algorithm is based on
java.util.TreeMap, but has been modified
to simultaneously map a tree node by key and by value. This doubles the
cost of put operations (but so does using two TreeMaps), and nearly doubles
the cost of remove operations (there is a savings in that the lookup of the
node to be removed only has to be performed once). And since only one node
contains the key and value, storage is significantly less than that
required by two TreeMaps.
The Map.Entry instances returned by the appropriate methods will
not allow setValue() and will throw an
UnsupportedOperationException on attempts to call that method.