Class TreeBasedTable<R,C,V>

All Implemented Interfaces:
RowSortedTable<R,C,V>, Table<R,C,V>, Serializable

public class TreeBasedTable<R,C,V> extends StandardRowSortedTable<R,C,V>
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 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:
  • Field Details

    • columnComparator

      private final Comparator<? super C> columnComparator
    • serialVersionUID

      private static final long serialVersionUID
      See Also:
  • Constructor Details

    • TreeBasedTable

      TreeBasedTable(Comparator<? super R> rowComparator, Comparator<? super C> columnComparator)
  • Method Details

    • create

      public static <R extends Comparable, C extends Comparable, 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.

    • create

      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 - the comparator that orders the row keys
      columnComparator - 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 a TreeBasedTable with the same mappings and sort order as the specified TreeBasedTable.
    • rowComparator

      @Deprecated 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.
    • columnComparator

      @Deprecated 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.
    • row

      public 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 a SortedMap, instead of the Map specified in the Table interface.

      Specified by:
      row in interface Table<R,C,V>
      Overrides:
      row in class StandardTable<R,C,V>
      Parameters:
      rowKey - key of row to search for in the table
      Returns:
      the corresponding map from column keys to values
      Since:
      10.0 (mostly source-compatible since 7.0)
    • rowKeySet

      public 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 the Set specified in the Table interface.

      Specified by:
      rowKeySet in interface RowSortedTable<R,C,V>
      Specified by:
      rowKeySet in interface Table<R,C,V>
      Overrides:
      rowKeySet in class StandardRowSortedTable<R,C,V>
      Returns:
      set of row keys
    • rowMap

      public SortedMap<R,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 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 Table.row(R). Those maps may support setValue(), put(), and putAll().

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

      Specified by:
      rowMap in interface RowSortedTable<R,C,V>
      Specified by:
      rowMap in interface Table<R,C,V>
      Overrides:
      rowMap in class StandardRowSortedTable<R,C,V>
      Returns:
      a map view from each row key to a secondary map from column keys to values
    • createColumnKeyIterator

      Iterator<C> createColumnKeyIterator()
      Overridden column iterator to return columns values in globally sorted order.
      Overrides:
      createColumnKeyIterator in class StandardTable<R,C,V>