Package org.apache.commons.collections4.bidimap

# Class DualTreeBidiMap<K,V>

java.lang.Object
org.apache.commons.collections4.bidimap.AbstractDualBidiMap<K,V>
org.apache.commons.collections4.bidimap.DualTreeBidiMap<K,V>

Type Parameters:
  `K` - the type of the keys in this map
  `V` - the type of the values in this map

All Implemented Interfaces:
  `Serializable`, `Map<K,V>`, `SortedMap<K,V>`, `BidiMap<K,V>`, `Get<K,V>`, `IterableGet<K,V>`, `IterableMap<K,V>`, `OrderedBidiMap<K,V>`, `OrderedMap<K,V>`, `Put<K,V>`, `SortedBidiMap<K,V>`

public class DualTreeBidiMap<K,V> extends AbstractDualBidiMap<K,V> implements SortedBidiMap<K,V>, Serializable
Implements `BidiMap` with two `TreeMap` instances.

The setValue() method on iterators will succeed only if the new value being set is not already in the bidi map.

When considering whether to use this class, the `TreeBidiMap` class should also be considered. It implements the interface using a dedicated design, and does not store each object twice, which can save on memory use.

NOTE: From Commons Collections 3.1, all subclasses will use `TreeMap` and the flawed `createMap` method is ignored.

Since:
  3.0
See Also:
  - Serialized Form

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `protected static class`
  `DualTreeBidiMap.BidiOrderedMapIterator<K,V>`
  Inner class MapIterator.
  `protected static class`
  `DualTreeBidiMap.ViewMap<K,V>`
  Internal sorted map view.

  ## Nested classes/interfaces inherited from class org.apache.commons.collections4.bidimap.AbstractDualBidiMap

  `AbstractDualBidiMap.BidiMapIterator<K,V>, AbstractDualBidiMap.EntrySet<K,V>, AbstractDualBidiMap.EntrySetIterator<K,V>, AbstractDualBidiMap.KeySet<K>, AbstractDualBidiMap.KeySetIterator<K>, AbstractDualBidiMap.MapEntry<K,V>, AbstractDualBidiMap.Values<V>, AbstractDualBidiMap.ValuesIterator<V>, AbstractDualBidiMap.View<K,V,E>`

  ## Nested classes/interfaces inherited from interface java.util.Map

  `Map.Entry<K,V>`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  ``
  `DualTreeBidiMap()`
  Creates an empty `DualTreeBidiMap`.
  ``
  `DualTreeBidiMap(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator)`
  Constructs a `DualTreeBidiMap` using the specified `Comparator`.
  ``
  `DualTreeBidiMap(Map<? extends K,? extends V> map)`
  Constructs a `DualTreeBidiMap` and copies the mappings from specified `Map`.
  `protected`
  `DualTreeBidiMap(Map<K,V> normalMap, Map<V,K> reverseMap, BidiMap<V,K> inverseBidiMap)`
  Constructs a `DualTreeBidiMap` that decorates the specified maps.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Comparator<? super K>`
  `comparator()`
  `protected DualTreeBidiMap<V,K>`
  `createBidiMap(Map<V,K> normalMap, Map<K,V> reverseMap, BidiMap<K,V> inverseMap)`
  Creates a new instance of this object.
  `K`
  `firstKey()`
  Gets the first key currently in this map.
  `SortedMap<K,V>`
  `headMap(K toKey)`
  `SortedBidiMap<V,K>`
  `inverseBidiMap()`
  Gets a view of this map where the keys and values are reversed.
  `OrderedBidiMap<V,K>`
  `inverseOrderedBidiMap()`
  Defaults to `inverseBidiMap()`.
  `SortedBidiMap<V,K>`
  `inverseSortedBidiMap()`
  Defaults to `inverseBidiMap()`.
  `K`
  `lastKey()`
  Gets the last key currently in this map.
  `OrderedMapIterator<K,V>`
  `mapIterator()`
  Obtains an ordered map iterator.
  `K`
  `nextKey(K key)`
  Gets the next key after the one specified.
  `K`
  `previousKey(K key)`
  Gets the previous key before the one specified.
  `SortedMap<K,V>`
  `subMap(K fromKey, K toKey)`
  `SortedMap<K,V>`
  `tailMap(K fromKey)`
  `Comparator<? super V>`
  `valueComparator()`
  Gets the comparator used for the values in the value-to-key map aspect.

  ### Methods inherited from class org.apache.commons.collections4.bidimap.AbstractDualBidiMap

  `clear, containsKey, containsValue, createEntrySetIterator, createKeySetIterator, createValuesIterator, entrySet, equals, get, getKey, hashCode, isEmpty, keySet, put, putAll, remove, removeValue, size, toString, values`

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface org.apache.commons.collections4.BidiMap

  `getKey, put, removeValue, values`

  ### Methods inherited from interface org.apache.commons.collections4.Get

  `containsKey, containsValue, entrySet, get, isEmpty, keySet, remove, size, values`

  ### Methods inherited from interface java.util.Map

  `clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, equals, forEach, get, getOrDefault, hashCode, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size`

  ### Methods inherited from interface org.apache.commons.collections4.Put

  `clear, put, putAll`

  ### Methods inherited from interface java.util.SortedMap

  `entrySet, keySet, values`

- ## Constructor Details

  - ### DualTreeBidiMap

    public DualTreeBidiMap()
    Creates an empty `DualTreeBidiMap`.

  - ### DualTreeBidiMap

    public DualTreeBidiMap(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator)
    Constructs a `DualTreeBidiMap` using the specified `Comparator`.
    Parameters:
      `keyComparator` - the comparator
      `valueComparator` - the values comparator to use

  - ### DualTreeBidiMap

    public DualTreeBidiMap(Map<? extends K,? extends V> map)
    Constructs a `DualTreeBidiMap` and copies the mappings from specified `Map`.
    Parameters:
      `map` - the map whose mappings are to be placed in this map

  - ### DualTreeBidiMap

    protected DualTreeBidiMap(Map<K,V> normalMap, Map<V,K> reverseMap, BidiMap<V,K> inverseBidiMap)
    Constructs a `DualTreeBidiMap` that decorates the specified maps.
    Parameters:
      `normalMap` - the normal direction map
      `reverseMap` - the reverse direction map
      `inverseBidiMap` - the inverse BidiMap

- ## Method Details

  - ### comparator

    public Comparator<? super K> comparator()
    Specified by:
      `comparator` in interface `SortedMap<K,V>`

  - ### createBidiMap

    protected DualTreeBidiMap<V,K> createBidiMap(Map<V,K> normalMap, Map<K,V> reverseMap, BidiMap<K,V> inverseMap)
    Creates a new instance of this object.
    Specified by:
      `createBidiMap` in class `AbstractDualBidiMap<K,V>`
    Parameters:
      `normalMap` - the normal direction map
      `reverseMap` - the reverse direction map
      `inverseMap` - the inverse BidiMap
    Returns:
      new bidi map

  - ### firstKey

    public K firstKey()
    Description copied from interface: `OrderedMap`
    Gets the first key currently in this map.
    Specified by:
      `firstKey` in interface `OrderedMap<K,V>`
    Specified by:
      `firstKey` in interface `SortedMap<K,V>`
    Returns:
      the first key currently in this map

  - ### headMap

    public SortedMap<K,V> headMap(K toKey)
    Specified by:
      `headMap` in interface `SortedMap<K,V>`

  - ### inverseBidiMap

    public SortedBidiMap<V,K> inverseBidiMap()
    Description copied from interface: `BidiMap`
    Gets a view of this map where the keys and values are reversed.

    Changes to one map will be visible in the other and vice versa. This enables both directions of the map to be accessed as a `Map`.

    Implementations should seek to avoid creating a new object every time this method is called. See `AbstractMap.values()` etc. Calling this method on the inverse map should return the original.

    Specified by:
      `inverseBidiMap` in interface `BidiMap<K,V>`
    Specified by:
      `inverseBidiMap` in interface `OrderedBidiMap<K,V>`
    Specified by:
      `inverseBidiMap` in interface `SortedBidiMap<K,V>`
    Overrides:
      `inverseBidiMap` in class `AbstractDualBidiMap<K,V>`
    Returns:
      an inverted bidirectional map

  - ### inverseOrderedBidiMap

    public OrderedBidiMap<V,K> inverseOrderedBidiMap()
    Defaults to `inverseBidiMap()`.
    Returns:
      Defaults to `inverseBidiMap()`.

  - ### inverseSortedBidiMap

    public SortedBidiMap<V,K> inverseSortedBidiMap()
    Defaults to `inverseBidiMap()`.
    Returns:
      Defaults to `inverseBidiMap()`.

  - ### lastKey

    public K lastKey()
    Description copied from interface: `OrderedMap`
    Gets the last key currently in this map.
    Specified by:
      `lastKey` in interface `OrderedMap<K,V>`
    Specified by:
      `lastKey` in interface `SortedMap<K,V>`
    Returns:
      the last key currently in this map

  - ### mapIterator

    public OrderedMapIterator<K,V> mapIterator()
    Obtains an ordered map iterator.

    This implementation copies the elements to an ArrayList in order to provide the forward/backward behavior.

    Specified by:
      `mapIterator` in interface `IterableGet<K,V>`
    Specified by:
      `mapIterator` in interface `OrderedMap<K,V>`
    Overrides:
      `mapIterator` in class `AbstractDualBidiMap<K,V>`
    Returns:
      a new ordered map iterator

  - ### nextKey

    public K nextKey(K key)
    Description copied from interface: `OrderedMap`
    Gets the next key after the one specified.
    Specified by:
      `nextKey` in interface `OrderedMap<K,V>`
    Parameters:
      `key` - the key to search for next from
    Returns:
      the next key, null if no match or at end

  - ### previousKey

    public K previousKey(K key)
    Description copied from interface: `OrderedMap`
    Gets the previous key before the one specified.
    Specified by:
      `previousKey` in interface `OrderedMap<K,V>`
    Parameters:
      `key` - the key to search for previous from
    Returns:
      the previous key, null if no match or at start

  - ### subMap

    public SortedMap<K,V> subMap(K fromKey, K toKey)
    Specified by:
      `subMap` in interface `SortedMap<K,V>`

  - ### tailMap

    public SortedMap<K,V> tailMap(K fromKey)
    Specified by:
      `tailMap` in interface `SortedMap<K,V>`

  - ### valueComparator

    public Comparator<? super V> valueComparator()
    Description copied from interface: `SortedBidiMap`
    Gets the comparator used for the values in the value-to-key map aspect.
    Specified by:
      `valueComparator` in interface `SortedBidiMap<K,V>`
    Returns:
      Comparator<? super V>