Top Description Inners Fields Constructors Methods
com.google.common.collect

public Class TreeBasedTable<R, C, V>

extends StandardRowSortedTable<R, C, V>
Class Inheritance
Annotations
@GwtCompatible
serializable:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, java.util.Objects.requireNonNull

Implementation of Table whose row keys and column keys are ordered by their natural ordering or by supplied comparators. When constructing a TreeBasedTable, you may provide comparators for the row keys and the column keys, or you may use natural ordering for both.

The rowKeySet method returns a SortedSet and the rowMap method returns a SortedMap, instead of the Set and Map specified by the Table interface.

The views returned by column, columnKeySet(), and columnMap() have iterators that don't support remove(). Otherwise, all optional operations are supported. Null row keys, columns keys, and values are not supported.

Lookups by row key are often faster than lookups by column key, because the data is stored in a Map<R, Map<C, V>>. A method call like column(columnKey).get(rowKey) still runs quickly, since the row key is provided. However, column(columnKey).size() takes longer, since an iteration across all row keys occurs.

Because a TreeBasedTable has unique sorted values for a given row, both row(rowKey) and rowMap().get(rowKey) are SortedMap instances, instead of the Map specified in the Table interface.

Note that this implementation is not synchronized. If multiple threads access this table concurrently and one of the threads modifies the table, it must be synchronized externally.

See the Guava User Guide article on Table.

Authors
Jared Levy, Louis Wasserman
Since
7.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private class

Field Summary

Modifier and TypeField and Description
private final Comparator<? super C>
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
TreeBasedTable(Comparator<? super R> rowComparator, Comparator<? super C> columnComparator)

Method Summary

Modifier and TypeMethod and Description
public Comparator<? super C>
columnComparator()

Deprecated Store the Comparator alongside the Table.
Returns the comparator that orders the columns.
public static <R extends Comparable<T>, C extends Comparable<T>, V> TreeBasedTable<R, C, V>
create()

Creates an empty TreeBasedTable that uses the natural orderings of both row and column keys.

public static <R, C, V> TreeBasedTable<R, C, V>
create(Comparator<? super R>
the comparator that orders the row keys
rowComparator
,
Comparator<? super C>
the comparator that orders the column keys
columnComparator
)

Creates an empty TreeBasedTable that is ordered by the specified comparators.

public static <R, C, V> TreeBasedTable<R, C, V>
create(TreeBasedTable<R, C, ? extends V> table)

Creates a TreeBasedTable with the same mappings and sort order as the specified TreeBasedTable.

pack-priv Iterator<C>
createColumnKeyIterator()

Overrides com.google.common.collect.StandardTable.createColumnKeyIterator.

Overridden column iterator to return columns values in globally sorted order.
public SortedMap<C, V>
row(R
key of row to search for in the table
rowKey
)

Overrides com.google.common.collect.StandardTable.row.

Implements com.google.common.collect.Table.row.

Returns a view of all mappings that have the given row key.
public Comparator<? super R>
rowComparator()

Deprecated Use table.rowKeySet().comparator() instead.
Returns the comparator that orders the rows.
public SortedSet<R>
public SortedMap<R, Map<C, V>>
rowMap()

Overrides com.google.common.collect.StandardRowSortedTable.rowMap.

Implements com.google.common.collect.RowSortedTable.rowMap, com.google.common.collect.Table.rowMap.

Returns a view that associates each row key with the corresponding map from column keys to values.
Inherited from com.google.common.collect.StandardRowSortedTable:
createRowMap

Field Detail

columnComparatorback to summary
private final Comparator<? super C> columnComparator
serialVersionUIDback to summary
private static final long serialVersionUID

Hides com.google.common.collect.StandardRowSortedTable.serialVersionUID.

Constructor Detail

TreeBasedTableback to summary
pack-priv TreeBasedTable(Comparator<? super R> rowComparator, Comparator<? super C> columnComparator)

Method Detail

columnComparatorback to summary
public Comparator<? super C> columnComparator()

Deprecated

Store the Comparator alongside the Table. Or, if you know that the Table contains at least one value, you can retrieve the Comparator with: ((SortedMap<C, V>) table.rowMap().values().iterator().next()).comparator();.

Returns the comparator that orders the columns. With natural ordering, Ordering#natural() is returned.

Annotations
@Deprecated
createback to summary
public static <R extends Comparable<T>, C extends Comparable<T>, V> TreeBasedTable<R, C, V> create()

Creates an empty TreeBasedTable that uses the natural orderings of both row and column keys.

The method signature specifies R extends Comparable with a raw Comparable, instead of R extends Comparable<? super R>, and the same for C. That's necessary to support classes defined without generics.

createback to summary
public static <R, C, V> TreeBasedTable<R, C, V> create(Comparator<? super R> rowComparator, Comparator<? super C> columnComparator)

Creates an empty TreeBasedTable that is ordered by the specified comparators.

Parameters
rowComparator:Comparator<? super R>

the comparator that orders the row keys

columnComparator:Comparator<? super C>

the comparator that orders the column keys

createback to summary
public static <R, C, V> TreeBasedTable<R, C, V> create(TreeBasedTable<R, C, ? extends V> table)

Creates a TreeBasedTable with the same mappings and sort order as the specified TreeBasedTable.

createColumnKeyIteratorback to summary
pack-priv Iterator<C> createColumnKeyIterator()

Overrides com.google.common.collect.StandardTable.createColumnKeyIterator.

Overridden column iterator to return columns values in globally sorted order.

Annotations
@Override
rowback to summary
public SortedMap<C, V> row(R rowKey)

Overrides com.google.common.collect.StandardTable.row.

Implements com.google.common.collect.Table.row.

Doc from com.google.common.collect.Table.row.

Returns a view of all mappings that have the given row key. For each row key / column key / value mapping in the table with that row key, the returned map associates the column key with the value. If no mappings in the table have the provided row key, an empty map is returned.

Changes to the returned map will update the underlying table, and vice versa.

Because a TreeBasedTable has unique sorted values for a given row, this method returns a SortedMap, instead of the Map specified in the Table interface.

Parameters
rowKey:R

key of row to search for in the table

Returns:SortedMap<C, V>

the corresponding map from column keys to values

Annotations
@Override
Since
10.0 (mostly source-compatible since 7.0)
rowComparatorback to summary
public Comparator<? super R> rowComparator()

Deprecated

Use table.rowKeySet().comparator() instead.

Returns the comparator that orders the rows. With natural ordering, Ordering#natural() is returned.

Annotations
@Deprecated
rowKeySetback to summary
public SortedSet<R> rowKeySet()

Overrides com.google.common.collect.StandardRowSortedTable.rowKeySet.

Implements com.google.common.collect.RowSortedTable.rowKeySet, com.google.common.collect.Table.rowKeySet.

Doc from com.google.common.collect.StandardRowSortedTable.rowKeySet.

Returns a set of row keys that have one or more values in the table. Changes to the set will update the underlying table, and vice versa.

This method returns a SortedSet, instead of the Set specified in the Table interface.

This method returns a SortedSet, instead of the Set specified in the Table interface.

Returns:SortedSet<R>

Doc from com.google.common.collect.Table.rowKeySet.

set of row keys

Annotations
@Override
rowMapback to summary
public SortedMap<R, Map<C, V>> rowMap()

Overrides com.google.common.collect.StandardRowSortedTable.rowMap.

Implements com.google.common.collect.RowSortedTable.rowMap, com.google.common.collect.Table.rowMap.

Doc from com.google.common.collect.StandardRowSortedTable.rowMap.

Returns a view that associates each row key with the corresponding map from column keys to values. Changes to the returned map will update this table. The returned map does not support put() or putAll(), or setValue() on its entries.

In contrast, the maps returned by rowMap().get() have the same behavior as those returned by row. Those maps may support setValue(), put(), and putAll().

This method returns a SortedMap, instead of the Map specified in the Table interface.

This method returns a SortedMap, instead of the Map specified in the Table interface.

Returns:SortedMap<R, Map<C, V>>

Doc from com.google.common.collect.Table.rowMap.

a map view from each row key to a secondary map from column keys to values

Annotations
@Override
com.google.common.collect back to summary

private Class TreeBasedTable.Factory<C, V>

extends Object
implements Supplier<TreeMap<C, V>>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, com.google.common.base.Supplier, java.util.function.Supplier

Field Summary

Modifier and TypeField and Description
pack-priv final Comparator<? super C>
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
Factory(Comparator<? super C> comparator)

Method Summary

Modifier and TypeMethod and Description
public TreeMap<C, V>
get()

Implements com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

comparatorback to summary
pack-priv final Comparator<? super C> comparator
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

Factoryback to summary
pack-priv Factory(Comparator<? super C> comparator)

Method Detail

getback to summary
public TreeMap<C, V> get()

Implements com.google.common.base.Supplier.get.

Doc from com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type. The returned object may or may not be a new instance, depending on the implementation.

Returns:TreeMap<C, V>

an instance of the appropriate type

Annotations
@Override
com.google.common.collect back to summary

private Class TreeBasedTable.TreeRow

extends StandardTable<R, C, V>.Row
implements SortedMap<C, V>
Class Inheritance
All Implemented Interfaces
java.util.SortedMap, java.util.SequencedMap, java.util.Map

Field Summary

Modifier and TypeField and Description
pack-priv final C
pack-priv final C
pack-priv transient SortedMap<C, V>
Inherited from com.google.common.collect.StandardTable.Row:
backingRowMaprowKey

Constructor Summary

AccessConstructor and Description
pack-priv
TreeRow(R rowKey)

pack-priv
TreeRow(R rowKey, C lowerBound, C upperBound)

Method Summary

Modifier and TypeMethod and Description
public Comparator<? super C>
comparator()

Implements java.util.SortedMap.comparator.

Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.
pack-priv int
pack-priv SortedMap<C, V>
public boolean
containsKey(Object
key whose presence in this map is to be tested
key
)

Overrides com.google.common.collect.StandardTable.Row.containsKey.

Implements java.util.Map.containsKey.

Returns true if this map contains a mapping for the specified key.
public C
firstKey()

Implements java.util.SortedMap.firstKey.

Returns the first (lowest) key currently in this map.
public SortedMap<C, V>
headMap(C
high endpoint (exclusive) of the keys in the returned map
toKey
)

Implements java.util.SortedMap.headMap.

Returns a view of the portion of this map whose keys are strictly less than toKey.
public SortedSet<C>
keySet()

Overrides java.util.AbstractMap.keySet.

Implements java.util.SortedMap.keySet, java.util.Map.keySet.

Returns a Set view of the keys contained in this map.
public C
lastKey()

Implements java.util.SortedMap.lastKey.

Returns the last (highest) key currently in this map.
pack-priv void
public V
put(C
key with which the specified value is to be associated
key
,
V
value to be associated with the specified key
value
)

Overrides com.google.common.collect.StandardTable.Row.put.

Implements java.util.Map.put.

Associates the specified value with the specified key in this map (optional operation).
pack-priv boolean
public SortedMap<C, V>
subMap(C
low endpoint (inclusive) of the keys in the returned map
fromKey
,
C
high endpoint (exclusive) of the keys in the returned map
toKey
)

Implements java.util.SortedMap.subMap.

Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
public SortedMap<C, V>
tailMap(C
low endpoint (inclusive) of the keys in the returned map
fromKey
)

Implements java.util.SortedMap.tailMap.

Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
pack-priv void
Inherited from com.google.common.collect.StandardTable.Row:
clearentryIteratorentrySpliteratorgetremovesizeupdateBackingRowMapFieldwrapEntry

Field Detail

lowerBoundback to summary
pack-priv final C lowerBound
Annotations
@CheckForNull
upperBoundback to summary
pack-priv final C upperBound
Annotations
@CheckForNull
wholeRowback to summary
pack-priv transient SortedMap<C, V> wholeRow
Annotations
@CheckForNull

Constructor Detail

TreeRowback to summary
pack-priv TreeRow(R rowKey)
TreeRowback to summary
pack-priv TreeRow(R rowKey, C lowerBound, C upperBound)

Method Detail

comparatorback to summary
public Comparator<? super C> comparator()

Implements java.util.SortedMap.comparator.

Doc from java.util.SortedMap.comparator.

Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.

Returns:Comparator<? super C>

the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys

Annotations
@Override
compareback to summary
pack-priv int compare(Object a, Object b)
computeBackingRowMapback to summary
pack-priv SortedMap<C, V> computeBackingRowMap()

Overrides com.google.common.collect.StandardTable.Row.computeBackingRowMap.

Annotations
@Override
@CheckForNull
containsKeyback to summary
public boolean containsKey(Object key)

Overrides com.google.common.collect.StandardTable.Row.containsKey.

Implements java.util.Map.containsKey.

Doc from java.util.Map.containsKey.

Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that Objects.equals(key, k). (There can be at most one such mapping.)

Parameters
key:Object

key whose presence in this map is to be tested

Returns:boolean

true if this map contains a mapping for the specified key

Annotations
@Override
firstKeyback to summary
public C firstKey()

Implements java.util.SortedMap.firstKey.

Doc from java.util.SortedMap.firstKey.

Returns the first (lowest) key currently in this map.

Returns:C

the first (lowest) key currently in this map

Annotations
@Override
headMapback to summary
public SortedMap<C, V> headMap(C toKey)

Implements java.util.SortedMap.headMap.

Doc from java.util.SortedMap.headMap.

Returns a view of the portion of this map whose keys are strictly less than toKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

Parameters
toKey:C

high endpoint (exclusive) of the keys in the returned map

Returns:SortedMap<C, V>

a view of the portion of this map whose keys are strictly less than toKey

Annotations
@Override
keySetback to summary
public SortedSet<C> keySet()

Overrides java.util.AbstractMap.keySet.

Implements java.util.SortedMap.keySet, java.util.Map.keySet.

Doc from java.util.SortedMap.keySet.

Returns a Set view of the keys contained in this map. The set's iterator returns the keys in ascending 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 (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Returns:SortedSet<C>

a set view of the keys contained in this map, sorted in ascending order

Annotations
@Override
lastKeyback to summary
public C lastKey()

Implements java.util.SortedMap.lastKey.

Doc from java.util.SortedMap.lastKey.

Returns the last (highest) key currently in this map.

Returns:C

the last (highest) key currently in this map

Annotations
@Override
maintainEmptyInvariantback to summary
pack-priv void maintainEmptyInvariant()

Overrides com.google.common.collect.StandardTable.Row.maintainEmptyInvariant.

Annotations
@Override
putback to summary
public V put(C key, V value)

Overrides com.google.common.collect.StandardTable.Row.put.

Implements java.util.Map.put.

Doc from java.util.Map.put.

Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

Parameters
key:C

key with which the specified value is to be associated

value:V

value to be associated with the specified key

Returns:V

the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)

Annotations
@Override
@CheckForNull
rangeContainsback to summary
pack-priv boolean rangeContains(Object o)
subMapback to summary
public SortedMap<C, V> subMap(C fromKey, C toKey)

Implements java.util.SortedMap.subMap.

Doc from java.util.SortedMap.subMap.

Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. (If fromKey and toKey are equal, the returned map is empty.) The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

Parameters
fromKey:C

low endpoint (inclusive) of the keys in the returned map

toKey:C

high endpoint (exclusive) of the keys in the returned map

Returns:SortedMap<C, V>

a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive

Annotations
@Override
tailMapback to summary
public SortedMap<C, V> tailMap(C fromKey)

Implements java.util.SortedMap.tailMap.

Doc from java.util.SortedMap.tailMap.

Returns a view of the portion of this map whose keys are greater than or equal to fromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

Parameters
fromKey:C

low endpoint (inclusive) of the keys in the returned map

Returns:SortedMap<C, V>

a view of the portion of this map whose keys are greater than or equal to fromKey

Annotations
@Override
updateWholeRowFieldback to summary
pack-priv void updateWholeRowField()