Class TreeBasedTable<R,C,V>
- java.lang.Object
-
- com.google.common.collect.AbstractTable<R,C,V>
-
- com.google.common.collect.StandardTable<R,C,V>
-
- com.google.common.collect.StandardRowSortedTable<R,C,V>
-
- com.google.common.collect.TreeBasedTable<R,C,V>
-
- All Implemented Interfaces:
RowSortedTable<R,C,V>
,Table<R,C,V>
,java.io.Serializable
@GwtCompatible(serializable=true) public class TreeBasedTable<R,C,V> extends StandardRowSortedTable<R,C,V>
Implementation ofTable
whose row keys and column keys are ordered by their natural ordering or by supplied comparators. When constructing aTreeBasedTable
, you may provide comparators for the row keys and the column keys, or you may use natural ordering for both.The
rowKeySet()
method returns aSortedSet
and therowMap()
method returns aSortedMap
, instead of theSet
andMap
specified by theTable
interface.The views returned by
StandardTable.column(C)
,StandardTable.columnKeySet()
, andStandardTable.columnMap()
have iterators that don't supportremove()
. 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 likecolumn(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, bothrow(rowKey)
androwMap().get(rowKey)
areSortedMap
instances, instead of theMap
specified in theTable
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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
TreeBasedTable.Factory<C,V>
private class
TreeBasedTable.TreeRow
-
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
Fields Modifier and Type Field Description private java.util.Comparator<? super C>
columnComparator
private static long
serialVersionUID
-
Fields inherited from class com.google.common.collect.StandardTable
backingMap, factory
-
-
Constructor Summary
Constructors Constructor Description TreeBasedTable(java.util.Comparator<? super R> rowComparator, java.util.Comparator<? super C> columnComparator)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.util.Comparator<? super C>
columnComparator()
Deprecated.Store theComparator
alongside theTable
.static <R extends java.lang.Comparable,C extends java.lang.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(java.util.Comparator<? super R> rowComparator, java.util.Comparator<? super C> columnComparator)
Creates an emptyTreeBasedTable
that is ordered by the specified comparators.(package private) java.util.Iterator<C>
createColumnKeyIterator()
Overridden column iterator to return columns values in globally sorted order.java.util.SortedMap<C,V>
row(R rowKey)
Returns a view of all mappings that have the given row key.java.util.Comparator<? super R>
rowComparator()
Deprecated.Usetable.rowKeySet().comparator()
instead.java.util.SortedSet<R>
rowKeySet()
Returns a set of row keys that have one or more values in the table.java.util.SortedMap<R,java.util.Map<C,V>>
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 Detail
-
columnComparator
private final java.util.Comparator<? super C> columnComparator
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Method Detail
-
create
public static <R extends java.lang.Comparable,C extends java.lang.Comparable,V> TreeBasedTable<R,C,V> 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,V> TreeBasedTable<R,C,V> create(java.util.Comparator<? super R> rowComparator, java.util.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
public 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
.
-
rowComparator
@Deprecated public java.util.Comparator<? super R> rowComparator()
Deprecated.Usetable.rowKeySet().comparator()
instead.Returns the comparator that orders the rows. With natural ordering,Ordering.natural()
is returned.
-
columnComparator
@Deprecated public java.util.Comparator<? super C> columnComparator()
Deprecated.Returns the comparator that orders the columns. With natural ordering,Ordering.natural()
is returned.
-
row
public java.util.SortedMap<C,V> row(R rowKey)
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
public java.util.SortedSet<R> rowKeySet()
Description copied from class:StandardRowSortedTable
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 theSet
specified in theTable
interface.
-
rowMap
public java.util.SortedMap<R,java.util.Map<C,V>> 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
java.util.Iterator<C> createColumnKeyIterator()
Overridden column iterator to return columns values in globally sorted order.- Overrides:
createColumnKeyIterator
in classStandardTable<R,C,V>
-
-