Class TreeBasedTable<R,C,V>
- All Implemented Interfaces:
RowSortedTable<R,
,C, V> Table<R,
,C, V> Serializable
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 StandardTable.column(C)
, StandardTable.columnKeySet()
, and StandardTable.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
.
- Since:
- 7.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
private class
Nested classes/interfaces inherited from class com.google.common.collect.StandardTable
StandardTable.Row, StandardTable.RowMap
Nested classes/interfaces inherited from class com.google.common.collect.AbstractTable
AbstractTable.CellSet, AbstractTable.Values
Nested classes/interfaces inherited from interface com.google.common.collect.Table
Table.Cell<R,
C, V> -
Field Summary
FieldsFields inherited from class com.google.common.collect.StandardTable
backingMap, factory
-
Constructor Summary
ConstructorsConstructorDescriptionTreeBasedTable
(Comparator<? super R> rowComparator, Comparator<? super C> columnComparator) -
Method Summary
Modifier and TypeMethodDescriptionComparator
<? super C> Deprecated.static <R extends Comparable,
C extends Comparable, V>
TreeBasedTable<R, C, V> create()
Creates an emptyTreeBasedTable
that uses the natural orderings of both row and column keys.static <R,
C, V> TreeBasedTable <R, C, V> create
(TreeBasedTable<R, C, ? extends V> table) Creates aTreeBasedTable
with the same mappings and sort order as the specifiedTreeBasedTable
.static <R,
C, V> TreeBasedTable <R, C, V> create
(Comparator<? super R> rowComparator, Comparator<? super C> columnComparator) Creates an emptyTreeBasedTable
that is ordered by the specified comparators.Overridden column iterator to return columns values in globally sorted order.Returns a view of all mappings that have the given row key.Comparator
<? super R> Deprecated.Usetable.rowKeySet().comparator()
instead.Returns a set of row keys that have one or more values in the table.rowMap()
Returns a view that associates each row key with the corresponding map from column keys to values.Methods inherited from class com.google.common.collect.StandardRowSortedTable
createRowMap
Methods inherited from class com.google.common.collect.StandardTable
cellIterator, cellSet, cellSpliterator, clear, column, columnKeySet, columnMap, contains, containsColumn, containsRow, containsValue, get, isEmpty, put, remove, size, values
Methods inherited from class com.google.common.collect.AbstractTable
createCellSet, createValues, equals, hashCode, putAll, toString, valuesIterator, valuesSpliterator
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.google.common.collect.Table
cellSet, clear, column, columnKeySet, columnMap, contains, containsColumn, containsRow, containsValue, equals, get, hashCode, isEmpty, put, putAll, remove, size, values
-
Field Details
-
columnComparator
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
-
Constructor Details
-
TreeBasedTable
TreeBasedTable(Comparator<? super R> rowComparator, Comparator<? super C> columnComparator)
-
-
Method Details
-
create
Creates an emptyTreeBasedTable
that uses the natural orderings of both row and column keys.The method signature specifies
R extends Comparable
with a rawComparable
, instead ofR extends Comparable<? super R>
, and the same forC
. That's necessary to support classes defined without generics. -
create
public static <R,C, TreeBasedTable<R,V> C, createV> (Comparator<? super R> rowComparator, Comparator<? super C> columnComparator) Creates an emptyTreeBasedTable
that is ordered by the specified comparators.- Parameters:
rowComparator
- the comparator that orders the row keyscolumnComparator
- the comparator that orders the column keys
-
create
Creates aTreeBasedTable
with the same mappings and sort order as the specifiedTreeBasedTable
. -
rowComparator
Deprecated.Usetable.rowKeySet().comparator()
instead.Returns the comparator that orders the rows. With natural ordering,Ordering.natural()
is returned. -
columnComparator
Deprecated.Store theComparator
alongside theTable
. Or, if you know that theTable
contains at least one value, you can retrieve theComparator
with:((SortedMap<C, V>) table.rowMap().values().iterator().next()).comparator();
.Returns the comparator that orders the columns. With natural ordering,Ordering.natural()
is returned. -
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 aSortedMap
, instead of theMap
specified in theTable
interface. -
rowKeySet
Description copied from class:StandardRowSortedTable
-
rowMap
Description copied from class:StandardRowSortedTable
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 supportput()
orputAll()
, orsetValue()
on its entries.In contrast, the maps returned by
rowMap().get()
have the same behavior as those returned byTable.row(R)
. Those maps may supportsetValue()
,put()
, andputAll()
.This method returns a
SortedMap
, instead of theMap
specified in theTable
interface. -
createColumnKeyIterator
Overridden column iterator to return columns values in globally sorted order.- Overrides:
createColumnKeyIterator
in classStandardTable<R,
C, V>
-
Comparator
alongside theTable
.