Top Description Inners Fields Constructors Methods
org.apache.commons.collections4.bidimap

public Class TreeBidiMap<K extends java.lang.Comparable<K>, V extends java.lang.Comparable<V>>

extends Object
implements OrderedBidiMap<K extends Comparable<K>, V extends Comparable<V>>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.apache.commons.collections4.OrderedBidiMap, org.apache.commons.collections4.OrderedMap, org.apache.commons.collections4.IterableMap, org.apache.commons.collections4.IterableGet, org.apache.commons.collections4.Get, org.apache.commons.collections4.Put, java.util.Map, org.apache.commons.collections4.BidiMap
Type Parameters
<K>
the type of the keys in this map
<V>
the type of the values in this map
Static Imports
org.apache.commons.collections4.bidimap.TreeBidiMap.DataElement.KEY, .TreeBidiMap.DataElement.VALUE

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
3.0 (previously DoubleOrderedMap v2.0)

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static enum
pack-priv class
TreeBidiMap.EntryView

A view of this map.

pack-priv class
TreeBidiMap.Inverse

The inverse map implementation.

pack-priv class
TreeBidiMap.InverseEntryView

A view of this map.

pack-priv class
TreeBidiMap.InverseViewMapEntryIterator

An iterator over the inverse map entries.

pack-priv class
TreeBidiMap.InverseViewMapIterator

An iterator over the map.

pack-priv class
pack-priv static class
TreeBidiMap.Node<K extends Comparable<K>, V extends Comparable<V>>

A node used to store the data.

pack-priv class
pack-priv abstract class
TreeBidiMap.View<E>

A view of this map.

pack-priv abstract class
TreeBidiMap.ViewIterator

An iterator over the map.

pack-priv class
TreeBidiMap.ViewMapEntryIterator

An iterator over the map entries.

pack-priv class
TreeBidiMap.ViewMapIterator

An iterator over the map.

Field Summary

Modifier and TypeField and Description
private transient Set<Map.Entry<K, V>>
private transient TreeBidiMap<K, V>.Inverse
private transient Set<K>
private transient int
private transient int
private transient TreeBidiMap.Node<K, V>[]
private static final long
private transient Set<V>

Constructor Summary

AccessConstructor and Description
public
TreeBidiMap()

Constructs a new empty TreeBidiMap.

public
TreeBidiMap(final Map<? extends K, ? extends V>
the map to copy
map
)

Constructs a new TreeBidiMap by copying an existing Map.

Method Summary

Modifier and TypeMethod and Description
private static void
checkKey(final Object
the key to be checked
key
)

check a key for validity (non-null and implements Comparable)

private static void
checkKeyAndValue(final Object
the key to be checked
key
,
final Object
the value to be checked
value
)

check a key and a value for validity (non-null and implements Comparable)

private static void
checkNonNullComparable(final Object
the object being checked
o
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

check if an object is fit to be proper input ...

private static void
checkValue(final Object
the value to be checked
value
)

check a value for validity (non-null and implements Comparable)

public void
clear()

Implements java.util.Map.clear, org.apache.commons.collections4.Put.clear.

Removes all mappings from this map.
private static <T extends Comparable<T>> int

Returns:

negative value if o1 < o2; 0 if o1 == o2; positive value if o1 > o2
compare
(final T
the first object
o1
,
final T
the second object
o2
)

Compare two objects

public boolean

Returns:

true if this map contains a mapping for the specified key
containsKey
(final Object
key whose presence in this map is to be tested
key
)

Implements java.util.Map.containsKey, org.apache.commons.collections4.Get.containsKey.

Checks whether this map contains the a mapping for the specified key.
public boolean

Returns:

true if this map contains a mapping for the specified value
containsValue
(final Object
value whose presence in this map is to be tested
value
)

Implements java.util.Map.containsValue, org.apache.commons.collections4.Get.containsValue.

Checks whether this map contains the a mapping for the specified value.
private void
copyColor(final TreeBidiMap.Node<K, V>
the node whose color we're copying; may be null
from
,
final TreeBidiMap.Node<K, V>
the node whose color we're changing; may be null
to
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

copy the color from one node to another, dealing with the fact that one or both nodes may, in fact, be null

private boolean

Returns:

true if equal
doEquals
(final Object
the object to compare to
obj
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

Compares for equals as per the API.

private int

Returns:

the hash code value for this map
doHashCode
(final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

Gets the hash code value for this map as per the API.

private void
doPut(final K
the key, always the main map key
key
,
final V
the value, always the main map value
value
)

Put logic.

private void
doRedBlackDelete(final TreeBidiMap.Node<K, V>
the node to be deleted
deletedNode
)

complicated red-black delete stuff.

private void
doRedBlackDeleteFixup(final TreeBidiMap.Node<K, V>
the node being replaced
replacementNode
,
final TreeBidiMap.DataElement
the KEY or VALUE int
dataElement
)

complicated red-black delete stuff.

private void
doRedBlackInsert(final TreeBidiMap.Node<K, V>
the node to be inserted
insertedNode
,
final TreeBidiMap.DataElement
the KEY or VALUE int
dataElement
)

complicated red-black insert stuff.

private V
doRemoveKey(final Object key)

private K
doRemoveValue(final Object value)

private String

Returns:

the string form of this map
doToString
(final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

Gets the string form of this map as per AbstractMap.

public Set<Map.Entry<K, V>>

Returns:

a set view of the values contained in this map.
entrySet
()

Implements java.util.Map.entrySet, org.apache.commons.collections4.Get.entrySet.

Returns a set view of the entries contained in this map in key order.
public boolean

Returns:

true if equal
equals
(final Object
the object to compare to
obj
)

Overrides java.lang.Object.equals.

Implements java.util.Map.equals.

Compares for equals as per the API.
public K

Returns:

the first (lowest) key currently in this sorted map
firstKey
()

Implements org.apache.commons.collections4.OrderedMap.firstKey.

Gets the first (lowest) key currently in this map.
public V

Returns:

the value to which this map maps the specified key, or null if the map contains no mapping for this key
get
(final Object
key whose associated value is to be returned
key
)

Implements java.util.Map.get, org.apache.commons.collections4.Get.get.

Gets the value to which this map maps the specified key.
private TreeBidiMap.Node<K, V>
getGrandParent(final TreeBidiMap.Node<K, V>
the node (may be null) in question
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

get a node's grandparent.

public K

Returns:

the key to which this map maps the specified value, or null if the map contains no mapping for this value.
getKey
(final Object
value whose associated key is to be returned.
value
)

Implements org.apache.commons.collections4.BidiMap.getKey.

Returns the key to which this map maps the specified value.
private TreeBidiMap.Node<K, V>
getLeftChild(final TreeBidiMap.Node<K, V>
the node (may be null) in question
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

get a node's left child.

private MapIterator<?, ?>
private TreeBidiMap.Node<K, V>
getParent(final TreeBidiMap.Node<K, V>
the node (may be null) in question
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

get a node's parent.

private TreeBidiMap.Node<K, V>
getRightChild(final TreeBidiMap.Node<K, V>
the node (may be null) in question
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

get a node's right child.

private TreeBidiMap.Node<K, V>

Returns:

the greatest node, from the specified node
greatestNode
(final TreeBidiMap.Node<K, V>
the node from which we will start searching
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

Find the greatest node from a given node.

private void
grow()

bump up the size and note that the map has changed

public int

Returns:

the hash code value for this map
hashCode
()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.hashCode.

Gets the hash code value for this map as per the API.
private void
insertValue(final TreeBidiMap.Node<K, V>
the node to be inserted
newNode
)

insert a node by its value

public OrderedBidiMap<V, K>

Returns:

the inverse map
inverseBidiMap
()

Implements org.apache.commons.collections4.OrderedBidiMap.inverseBidiMap.

Gets the inverse map for comparison.
private static boolean
isBlack(final TreeBidiMap.Node<?, ?>
the node (may be null) in question
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

is the specified black red?

public boolean

Returns:

true if the map is empty
isEmpty
()

Implements java.util.Map.isEmpty, org.apache.commons.collections4.Get.isEmpty.

Checks whether the map is empty or not.
private static boolean
isRed(final TreeBidiMap.Node<?, ?>
the node (may be null) in question
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

is the specified node red?

public Set<K>

Returns:

a set view of the keys contained in this map.
keySet
()

Implements java.util.Map.keySet, org.apache.commons.collections4.Get.keySet.

Returns a set view of the keys contained in this map in key order.
public K

Returns:

the last (highest) key currently in this sorted map
lastKey
()

Implements org.apache.commons.collections4.OrderedMap.lastKey.

Gets the last (highest) key currently in this map.
private TreeBidiMap.Node<K, V>

Returns:

the smallest node, from the specified node, in the specified mapping
leastNode
(final TreeBidiMap.Node<K, V>
the node from which we will start searching
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

Find the least node from a given node.

private <T extends Comparable<T>> TreeBidiMap.Node<K, V>

Returns:

the desired Node, or null if there is no mapping of the specified data
lookup
(final Object
the key or value to be looked up
data
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

do the actual lookup of a piece of data

private TreeBidiMap.Node<K, V>
lookupKey(final Object key)

private TreeBidiMap.Node<K, V>
lookupValue(final Object value)

private static void
makeBlack(final TreeBidiMap.Node<?, ?>
the node (may be null) in question
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

force a node (if it exists) black

private static void
makeRed(final TreeBidiMap.Node<?, ?>
the node (may be null) in question
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

force a node (if it exists) red

public OrderedMapIterator<K, V>
private void
modify()

increment the modification count -- used to check for concurrent modification of the map through the map and through an Iterator from one of its Set or Collection views

private TreeBidiMap.Node<K, V>

Returns:

the specified node
nextGreater
(final TreeBidiMap.Node<K, V>
the node to be searched from
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

get the next larger node from the specified node

public K

Returns:

the next key, null if no match or at end
nextKey
(final K
the key to search for next from
key
)

Implements org.apache.commons.collections4.OrderedMap.nextKey.

Gets the next key after the one specified.
private TreeBidiMap.Node<K, V>

Returns:

the specified node
nextSmaller
(final TreeBidiMap.Node<K, V>
the node to be searched from
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

get the next larger node from the specified node

public K

Returns:

the previous key, null if no match or at start
previousKey
(final K
the key to search for previous from
key
)

Implements org.apache.commons.collections4.OrderedMap.previousKey.

Gets the previous key before the one specified.
public V

Returns:

the previous value for the key
put
(final K
key with which the specified value is to be associated
key
,
final V
value to be associated with the specified key
value
)

Implements org.apache.commons.collections4.BidiMap.put, java.util.Map.put, org.apache.commons.collections4.Put.put.

Puts the key-value pair into the map, replacing any previous pair.
public void
putAll(final Map<? extends K, ? extends V>
the map to copy from
map
)

Implements java.util.Map.putAll, org.apache.commons.collections4.Put.putAll.

Puts all the mappings from the specified map into this map.
private void
readObject(final ObjectInputStream
the input stream
stream
)

Reads the content of the stream.

public V

Returns:

previous value associated with specified key, or null if there was no mapping for key.
remove
(final Object
key whose mapping is to be removed from the map.
key
)

Implements java.util.Map.remove, org.apache.commons.collections4.Get.remove.

Removes the mapping for this key from this map if present.
public K

Returns:

previous key associated with specified value, or null if there was no mapping for value.
removeValue
(final Object
value whose mapping is to be removed from the map
value
)

Implements org.apache.commons.collections4.BidiMap.removeValue.

Removes the mapping for this value from this map if present.
private void
rotateLeft(final TreeBidiMap.Node<K, V>
the node to be rotated
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

do a rotate left.

private void
rotateRight(final TreeBidiMap.Node<K, V>
the node to be rotated
node
,
final TreeBidiMap.DataElement
either DataElement#KEY key} or the value.
dataElement
)

do a rotate right.

private void
shrink()

decrement the size and note that the map has changed

public int

Returns:

the number of key-value mappings in this map
size
()

Implements java.util.Map.size, org.apache.commons.collections4.Get.size.

Returns the number of key-value mappings in this map.
private void
swapPosition(final TreeBidiMap.Node<K, V>
one node
x
,
final TreeBidiMap.Node<K, V>
another node
y
,
final TreeBidiMap.DataElement
the KEY or VALUE int
dataElement
)

swap two nodes (except for their content), taking care of special cases where one is the other's parent ...

public String

Returns:

a standard format string version of the map
toString
()

Overrides java.lang.Object.toString.

Returns a string version of this Map in standard format.
public Set<V>

Returns:

a set view of the values contained in this map.
values
()

Implements org.apache.commons.collections4.BidiMap.values, java.util.Map.values, org.apache.commons.collections4.Get.values.

Returns a set view of the values contained in this map in key order.
private void
writeObject(final ObjectOutputStream
the output stream
stream
)

Writes the content to the stream for serialization.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait