Class TreeBidiMap

  • All Implemented Interfaces:
    java.util.Map, BidiMap, IterableMap, OrderedBidiMap, OrderedMap

    @Deprecated(since="2021-04-30")
    public class TreeBidiMap
    extends java.lang.Object
    implements OrderedBidiMap
    Deprecated.
    Commons Collections 3 is in maintenance mode. Commons Collections 4 should be used instead.
    Red-Black tree-based implementation of BidiMap where all objects added implement the Comparable interface.

    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 DualTreeBidiMap and 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.

    Since:
    Commons Collections 3.0 (previously DoubleOrderedMap v2.0)
    • Nested Class Summary

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

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      TreeBidiMap()
      Deprecated.
      Constructs a new empty TreeBidiMap.
      TreeBidiMap​(java.util.Map map)
      Deprecated.
      Constructs a new TreeBidiMap by copying an existing Map.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void clear()
      Deprecated.
      Removes all mappings from this map.
      boolean containsKey​(java.lang.Object key)
      Deprecated.
      Checks whether this map contains the a mapping for the specified key.
      boolean containsValue​(java.lang.Object value)
      Deprecated.
      Checks whether this map contains the a mapping for the specified value.
      java.util.Set entrySet()
      Deprecated.
      Returns a set view of the entries contained in this map in key order.
      boolean equals​(java.lang.Object obj)
      Deprecated.
      Compares for equals as per the API.
      java.lang.Object firstKey()
      Deprecated.
      Gets the first (lowest) key currently in this map.
      java.lang.Object get​(java.lang.Object key)
      Deprecated.
      Gets the value to which this map maps the specified key.
      java.lang.Object getKey​(java.lang.Object value)
      Deprecated.
      Returns the key to which this map maps the specified value.
      int hashCode()
      Deprecated.
      Gets the hash code value for this map as per the API.
      BidiMap inverseBidiMap()
      Deprecated.
      Gets the inverse map for comparison.
      OrderedBidiMap inverseOrderedBidiMap()
      Deprecated.
      Gets the inverse map for comparison.
      boolean isEmpty()
      Deprecated.
      Checks whether the map is empty or not.
      java.util.Set keySet()
      Deprecated.
      Returns a set view of the keys contained in this map in key order.
      java.lang.Object lastKey()
      Deprecated.
      Gets the last (highest) key currently in this map.
      MapIterator mapIterator()
      Deprecated.
      Gets an iterator over the map entries.
      java.lang.Object nextKey​(java.lang.Object key)
      Deprecated.
      Gets the next key after the one specified.
      OrderedMapIterator orderedMapIterator()
      Deprecated.
      Gets an ordered iterator over the map entries.
      java.lang.Object previousKey​(java.lang.Object key)
      Deprecated.
      Gets the previous key before the one specified.
      java.lang.Object put​(java.lang.Object key, java.lang.Object value)
      Deprecated.
      Puts the key-value pair into the map, replacing any previous pair.
      void putAll​(java.util.Map map)
      Deprecated.
      Puts all the mappings from the specified map into this map.
      java.lang.Object remove​(java.lang.Object key)
      Deprecated.
      Removes the mapping for this key from this map if present.
      java.lang.Object removeValue​(java.lang.Object value)
      Deprecated.
      Removes the mapping for this value from this map if present.
      int size()
      Deprecated.
      Returns the number of key-value mappings in this map.
      java.lang.String toString()
      Deprecated.
      Returns a string version of this Map in standard format.
      java.util.Collection values()
      Deprecated.
      Returns a set view of the values contained in this map in key order.
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • TreeBidiMap

        public TreeBidiMap()
        Deprecated.
        Constructs a new empty TreeBidiMap.
      • TreeBidiMap

        public TreeBidiMap​(java.util.Map map)
        Deprecated.
        Constructs a new TreeBidiMap by copying an existing Map.
        Parameters:
        map - the map to copy
        Throws:
        java.lang.ClassCastException - if the keys/values in the map are not Comparable or are not mutually comparable
        java.lang.NullPointerException - if any key or value in the map is null
    • Method Detail

      • size

        public int size()
        Deprecated.
        Returns the number of key-value mappings in this map.
        Specified by:
        size in interface java.util.Map
        Returns:
        the number of key-value mappings in this map
      • isEmpty

        public boolean isEmpty()
        Deprecated.
        Checks whether the map is empty or not.
        Specified by:
        isEmpty in interface java.util.Map
        Returns:
        true if the map is empty
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Deprecated.
        Checks whether this map contains the a mapping for the specified key.

        The key must implement Comparable.

        Specified by:
        containsKey in interface java.util.Map
        Parameters:
        key - key whose presence in this map is to be tested
        Returns:
        true if this map contains a mapping for the specified key
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type
        java.lang.NullPointerException - if the key is null
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Deprecated.
        Checks whether this map contains the a mapping for the specified value.

        The value must implement Comparable.

        Specified by:
        containsValue in interface java.util.Map
        Parameters:
        value - value whose presence in this map is to be tested
        Returns:
        true if this map contains a mapping for the specified value
        Throws:
        java.lang.ClassCastException - if the value is of an inappropriate type
        java.lang.NullPointerException - if the value is null
      • get

        public java.lang.Object get​(java.lang.Object key)
        Deprecated.
        Gets the value to which this map maps the specified key. Returns null if the map contains no mapping for this key.

        The key must implement Comparable.

        Specified by:
        get in interface java.util.Map
        Parameters:
        key - key whose associated value is to be returned
        Returns:
        the value to which this map maps the specified key, or null if the map contains no mapping for this key
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type
        java.lang.NullPointerException - if the key is null
      • put

        public java.lang.Object put​(java.lang.Object key,
                                    java.lang.Object value)
        Deprecated.
        Puts the key-value pair into the map, replacing any previous pair.

        When adding a key-value pair, the value may already exist in the map against a different key. That mapping is removed, to ensure that the value only occurs once in the inverse map.

          BidiMap map1 = new TreeBidiMap();
          map.put("A","B");  // contains A mapped to B, as per Map
          map.put("A","C");  // contains A mapped to C, as per Map
        
          BidiMap map2 = new TreeBidiMap();
          map.put("A","B");  // contains A mapped to B, as per Map
          map.put("C","B");  // contains C mapped to B, key A is removed
         

        Both key and value must implement Comparable.

        Specified by:
        put in interface BidiMap
        Specified by:
        put in interface java.util.Map
        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
        Returns:
        the previous value for the key
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type
        java.lang.NullPointerException - if the key is null
      • putAll

        public void putAll​(java.util.Map map)
        Deprecated.
        Puts all the mappings from the specified map into this map.

        All keys and values must implement Comparable.

        Specified by:
        putAll in interface java.util.Map
        Parameters:
        map - the map to copy from
      • remove

        public java.lang.Object remove​(java.lang.Object key)
        Deprecated.
        Removes the mapping for this key from this map if present.

        The key must implement Comparable.

        Specified by:
        remove in interface java.util.Map
        Parameters:
        key - key whose mapping is to be removed from the map.
        Returns:
        previous value associated with specified key, or null if there was no mapping for key.
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type
        java.lang.NullPointerException - if the key is null
      • clear

        public void clear()
        Deprecated.
        Removes all mappings from this map.
        Specified by:
        clear in interface java.util.Map
      • getKey

        public java.lang.Object getKey​(java.lang.Object value)
        Deprecated.
        Returns the key to which this map maps the specified value. Returns null if the map contains no mapping for this value.

        The value must implement Comparable.

        Specified by:
        getKey in interface BidiMap
        Parameters:
        value - value whose associated key is to be returned.
        Returns:
        the key to which this map maps the specified value, or null if the map contains no mapping for this value.
        Throws:
        java.lang.ClassCastException - if the value is of an inappropriate type
        java.lang.NullPointerException - if the value is null
      • removeValue

        public java.lang.Object removeValue​(java.lang.Object value)
        Deprecated.
        Removes the mapping for this value from this map if present.

        The value must implement Comparable.

        Specified by:
        removeValue in interface BidiMap
        Parameters:
        value - value whose mapping is to be removed from the map
        Returns:
        previous key associated with specified value, or null if there was no mapping for value.
        Throws:
        java.lang.ClassCastException - if the value is of an inappropriate type
        java.lang.NullPointerException - if the value is null
      • firstKey

        public java.lang.Object firstKey()
        Deprecated.
        Gets the first (lowest) key currently in this map.
        Specified by:
        firstKey in interface OrderedMap
        Returns:
        the first (lowest) key currently in this sorted map
        Throws:
        java.util.NoSuchElementException - if this map is empty
      • lastKey

        public java.lang.Object lastKey()
        Deprecated.
        Gets the last (highest) key currently in this map.
        Specified by:
        lastKey in interface OrderedMap
        Returns:
        the last (highest) key currently in this sorted map
        Throws:
        java.util.NoSuchElementException - if this map is empty
      • nextKey

        public java.lang.Object nextKey​(java.lang.Object key)
        Deprecated.
        Gets the next key after the one specified.

        The key must implement Comparable.

        Specified by:
        nextKey in interface OrderedMap
        Parameters:
        key - the key to search for next from
        Returns:
        the next key, null if no match or at end
      • previousKey

        public java.lang.Object previousKey​(java.lang.Object key)
        Deprecated.
        Gets the previous key before the one specified.

        The key must implement Comparable.

        Specified by:
        previousKey in interface OrderedMap
        Parameters:
        key - the key to search for previous from
        Returns:
        the previous key, null if no match or at start
      • keySet

        public java.util.Set keySet()
        Deprecated.
        Returns a set view of the keys contained in this map in key order.

        The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined.

        The set supports element removal, which removes the corresponding mapping from the map. It does not support the add or addAll operations.

        Specified by:
        keySet in interface java.util.Map
        Returns:
        a set view of the keys contained in this map.
      • values

        public java.util.Collection values()
        Deprecated.
        Returns a set view of the values contained in this map in key order. The returned object can be cast to a Set.

        The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined.

        The set supports element removal, which removes the corresponding mapping from the map. It does not support the add or addAll operations.

        Specified by:
        values in interface java.util.Map
        Returns:
        a set view of the values contained in this map.
      • entrySet

        public java.util.Set entrySet()
        Deprecated.
        Returns a set view of the entries contained in this map in key order. For simple iteration through the map, the MapIterator is quicker.

        The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined.

        The set supports element removal, which removes the corresponding mapping from the map. It does not support the add or addAll operations. The returned MapEntry objects do not support setValue.

        Specified by:
        entrySet in interface java.util.Map
        Returns:
        a set view of the values contained in this map.
      • mapIterator

        public MapIterator mapIterator()
        Deprecated.
        Gets an iterator over the map entries.

        For this map, this iterator is the fastest way to iterate over the entries.

        Specified by:
        mapIterator in interface BidiMap
        Specified by:
        mapIterator in interface IterableMap
        Returns:
        an iterator
      • orderedMapIterator

        public OrderedMapIterator orderedMapIterator()
        Deprecated.
        Gets an ordered iterator over the map entries.

        This iterator allows both forward and reverse iteration over the entries.

        Specified by:
        orderedMapIterator in interface OrderedMap
        Returns:
        an iterator
      • equals

        public boolean equals​(java.lang.Object obj)
        Deprecated.
        Compares for equals as per the API.
        Specified by:
        equals in interface java.util.Map
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the object to compare to
        Returns:
        true if equal
      • hashCode

        public int hashCode()
        Deprecated.
        Gets the hash code value for this map as per the API.
        Specified by:
        hashCode in interface java.util.Map
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code value for this map
      • toString

        public java.lang.String toString()
        Deprecated.
        Returns a string version of this Map in standard format.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a standard format string version of the map