Class ArrayTable<R,C,V>

java.lang.Object
com.google.common.collect.ArrayTable<R,C,V>
All Implemented Interfaces:
Table<R,C,V>, Serializable

@GwtCompatible(emulated=true) public final class ArrayTable<R,C,V> extends Object implements Serializable
Fixed-size Table implementation backed by a two-dimensional array.

Warning: ArrayTable is rarely the Table implementation you want. First, it requires that the complete universe of rows and columns be specified at construction time. Second, it is always backed by an array large enough to hold a value for every possible combination of row and column keys. (This is rarely optimal unless the table is extremely dense.) Finally, every possible combination of row and column keys is always considered to have a value associated with it: It is not possible to "remove" a value, only to replace it with null, which will still appear when iterating over the table's contents in a foreach loop or a call to a null-hostile method like ImmutableTable.copyOf(com.google.common.collect.Table<? extends R, ? extends C, ? extends V>). For alternatives, please see the wiki.

The allowed row and column keys must be supplied when the table is created. The table always contains a mapping for every row key / column pair. The value corresponding to a given row and column is null unless another value is provided.

The table's size is constant: the product of the number of supplied row keys and the number of supplied column keys. The remove and clear methods are not supported by the table or its views. The erase(java.lang.Object, java.lang.Object) and eraseAll() methods may be used instead.

The ordering of the row and column keys provided when the table is constructed determines the iteration ordering across rows and columns in the table's views. None of the view iterators support Iterator.remove(). If the table is modified after an iterator is created, the iterator remains valid.

This class requires less memory than the HashBasedTable and TreeBasedTable implementations, except when the table is sparse.

Null row keys or column keys are not permitted.

This class provides methods involving the underlying array structure, where the array indices correspond to the position of a row or column in the lists of allowed keys and values. See the at(int, int), set(int, int, V), toArray(java.lang.Class<V>), rowKeyList(), and columnKeyList() methods for more details.

Note that this implementation is not synchronized. If multiple threads access the same cell of an ArrayTable concurrently and one of the threads modifies its value, there is no guarantee that the new value will be fully visible to the other threads. To guarantee that modifications are visible, synchronize access to the table. Unlike other Table implementations, synchronization is unnecessary between a thread that writes to one cell and a thread that reads from another.

See the Guava User Guide article on Table.

Since:
10.0
Author:
Jared Levy
See Also:
  • Method Details

    • create

      public static <R, C, V> ArrayTable<R,C,V> create(Iterable<? extends R> rowKeys, Iterable<? extends C> columnKeys)
      Creates an ArrayTable filled with null.
      Parameters:
      rowKeys - row keys that may be stored in the generated table
      columnKeys - column keys that may be stored in the generated table
      Throws:
      NullPointerException - if any of the provided keys is null
      IllegalArgumentException - if rowKeys or columnKeys contains duplicates or if exactly one of rowKeys or columnKeys is empty.
    • create

      public static <R, C, V> ArrayTable<R,C,V> create(Table<R,C,? extends @Nullable V> table)
      Creates an ArrayTable with the mappings in the provided table.

      If table includes a mapping with row key r and a separate mapping with column key c, the returned table contains a mapping with row key r and column key c. If that row key / column key pair in not in table, the pair maps to null in the generated table.

      The returned table allows subsequent put calls with the row keys in table.rowKeySet() and the column keys in table.columnKeySet(). Calling put(R, C, V) with other keys leads to an IllegalArgumentException.

      The ordering of table.rowKeySet() and table.columnKeySet() determines the row and column iteration ordering of the returned table.

      Throws:
      NullPointerException - if table has a null key
    • rowKeyList

      Returns, as an immutable list, the row keys provided when the table was constructed, including those that are mapped to null values only.
    • columnKeyList

      Returns, as an immutable list, the column keys provided when the table was constructed, including those that are mapped to null values only.
    • at

      public @Nullable V at(int rowIndex, int columnIndex)
      Returns the value corresponding to the specified row and column indices. The same value is returned by get(rowKeyList().get(rowIndex), columnKeyList().get(columnIndex)), but this method runs more quickly.
      Parameters:
      rowIndex - position of the row key in rowKeyList()
      columnIndex - position of the row key in columnKeyList()
      Returns:
      the value with the specified row and column
      Throws:
      IndexOutOfBoundsException - if either index is negative, rowIndex is greater than or equal to the number of allowed row keys, or columnIndex is greater than or equal to the number of allowed column keys
    • set

      @CanIgnoreReturnValue public @Nullable V set(int rowIndex, int columnIndex, @Nullable V value)
      Associates value with the specified row and column indices. The logic put(rowKeyList().get(rowIndex), columnKeyList().get(columnIndex), value) has the same behavior, but this method runs more quickly.
      Parameters:
      rowIndex - position of the row key in rowKeyList()
      columnIndex - position of the row key in columnKeyList()
      value - value to store in the table
      Returns:
      the previous value with the specified row and column
      Throws:
      IndexOutOfBoundsException - if either index is negative, rowIndex is greater than or equal to the number of allowed row keys, or columnIndex is greater than or equal to the number of allowed column keys
    • toArray

      @GwtIncompatible public @Nullable V[][] toArray(Class<V> valueClass)
      Returns a two-dimensional array with the table contents. The row and column indices correspond to the positions of the row and column in the iterables provided during table construction. If the table lacks a mapping for a given row and column, the corresponding array element is null.

      Subsequent table changes will not modify the array, and vice versa.

      Parameters:
      valueClass - class of values stored in the returned array
    • clear

      @Deprecated public void clear()
      Deprecated.
      Not supported. Use eraseAll() instead.
      Specified by:
      clear in interface Table<R,C,V>
      Throws:
      UnsupportedOperationException - always
    • eraseAll

      public void eraseAll()
      Associates the value null with every pair of allowed row and column keys.
    • contains

      public boolean contains(@Nullable Object rowKey, @Nullable Object columnKey)
      Returns true if the provided keys are among the keys provided when the table was constructed.
      Specified by:
      contains in interface Table<R,C,V>
      Parameters:
      rowKey - key of row to search for
      columnKey - key of column to search for
    • containsColumn

      public boolean containsColumn(@Nullable Object columnKey)
      Returns true if the provided column key is among the column keys provided when the table was constructed.
      Specified by:
      containsColumn in interface Table<R,C,V>
      Parameters:
      columnKey - key of column to search for
    • containsRow

      public boolean containsRow(@Nullable Object rowKey)
      Returns true if the provided row key is among the row keys provided when the table was constructed.
      Specified by:
      containsRow in interface Table<R,C,V>
      Parameters:
      rowKey - key of row to search for
    • containsValue

      public boolean containsValue(@Nullable Object value)
      Description copied from interface: Table
      Returns true if the table contains a mapping with the specified value.
      Specified by:
      containsValue in interface Table<R,C,V>
      Parameters:
      value - value to search for
    • get

      public @Nullable V get(@Nullable Object rowKey, @Nullable Object columnKey)
      Description copied from interface: Table
      Returns the value corresponding to the given row and column keys, or null if no such mapping exists.
      Specified by:
      get in interface Table<R,C,V>
      Parameters:
      rowKey - key of row to search for
      columnKey - key of column to search for
    • isEmpty

      public boolean isEmpty()
      Returns true if rowKeyList().size == 0 or columnKeyList().size() == 0.
      Specified by:
      isEmpty in interface Table<R,C,V>
    • put

      @CanIgnoreReturnValue public @Nullable V put(R rowKey, C columnKey, @Nullable V value)
      Associates the specified value with the specified keys. If the table already contained a mapping for those keys, the old value is replaced with the specified value.
      Specified by:
      put in interface Table<R,C,V>
      Parameters:
      rowKey - row key that the value should be associated with
      columnKey - column key that the value should be associated with
      value - value to be associated with the specified keys
      Returns:
      the value previously associated with the keys, or null if no mapping existed for the keys
      Throws:
      IllegalArgumentException - if rowKey is not in rowKeySet() or columnKey is not in columnKeySet().
    • putAll

      public void putAll(Table<? extends R,? extends C,? extends @Nullable V> table)
      Copies all mappings from the specified table to this table. The effect is equivalent to calling Table.put(R, C, V) with each row key / column key / value mapping in table.

      If table is an ArrayTable, its null values will be stored in this table, possibly replacing values that were previously non-null.

      Specified by:
      putAll in interface Table<R,C,V>
      Parameters:
      table - the table to add to this table
      Throws:
      NullPointerException - if table has a null key
      IllegalArgumentException - if any of the provided table's row keys or column keys is not in rowKeySet() or columnKeySet()
    • remove

      Not supported. Use erase(java.lang.Object, java.lang.Object) instead.
      Specified by:
      remove in interface Table<R,C,V>
      Parameters:
      rowKey - row key of mapping to be removed
      columnKey - column key of mapping to be removed
      Returns:
      the value previously associated with the keys, or null if no such value existed
      Throws:
      UnsupportedOperationException - always
    • erase

      Associates the value null with the specified keys, assuming both keys are valid. If either key is null or isn't among the keys provided during construction, this method has no effect.

      This method is equivalent to put(rowKey, columnKey, null) when both provided keys are valid.

      Parameters:
      rowKey - row key of mapping to be erased
      columnKey - column key of mapping to be erased
      Returns:
      the value previously associated with the keys, or null if no mapping existed for the keys
    • size

      public int size()
      Description copied from interface: Table
      Returns the number of row key / column key / value mappings in the table.
      Specified by:
      size in interface Table<R,C,V>
    • cellSet

      Returns an unmodifiable set of all row key / column key / value triplets. Changes to the table will update the returned set.

      The returned set's iterator traverses the mappings with the first row key, the mappings with the second row key, and so on.

      The value in the returned cells may change if the table subsequently changes.

      Specified by:
      cellSet in interface Table<R,C,V>
      Returns:
      set of table cells consisting of row key / column key / value triplets
    • column

      public Map<R,@Nullable V> column(C columnKey)
      Returns a view of all mappings that have the given column key. If the column key isn't in columnKeySet(), an empty immutable map is returned.

      Otherwise, for each row key in rowKeySet(), the returned map associates the row key with the corresponding value in the table. Changes to the returned map will update the underlying table, and vice versa.

      Specified by:
      column in interface Table<R,C,V>
      Parameters:
      columnKey - key of column to search for in the table
      Returns:
      the corresponding map from row keys to values
    • columnKeySet

      Returns an immutable set of the valid column keys, including those that are associated with null values only.
      Specified by:
      columnKeySet in interface Table<R,C,V>
      Returns:
      immutable set of column keys
    • columnMap

      public Map<C,Map<R,@Nullable V>> columnMap()
      Description copied from interface: Table
      Returns a view that associates each column key with the corresponding map from row 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 columnMap().get() have the same behavior as those returned by Table.column(C). Those maps may support setValue(), put(), and putAll().

      Specified by:
      columnMap in interface Table<R,C,V>
      Returns:
      a map view from each column key to a secondary map from row keys to values
    • row

      public Map<C,@Nullable V> row(R rowKey)
      Returns a view of all mappings that have the given row key. If the row key isn't in rowKeySet(), an empty immutable map is returned.

      Otherwise, for each column key in columnKeySet(), the returned map associates the column key with the corresponding value in the table. Changes to the returned map will update the underlying table, and vice versa.

      Specified by:
      row in interface Table<R,C,V>
      Parameters:
      rowKey - key of row to search for in the table
      Returns:
      the corresponding map from column keys to values
    • rowKeySet

      Returns an immutable set of the valid row keys, including those that are associated with null values only.
      Specified by:
      rowKeySet in interface Table<R,C,V>
      Returns:
      immutable set of row keys
    • rowMap

      public Map<R,Map<C,@Nullable V>> rowMap()
      Description copied from interface: Table
      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().

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

      Returns an unmodifiable collection of all values, which may contain duplicates. Changes to the table will update the returned collection.

      The returned collection's iterator traverses the values of the first row key, the values of the second row key, and so on.

      Specified by:
      values in interface Table<R,C,V>
      Returns:
      collection of values
    • equals

      public boolean equals(@Nullable Object obj)
      Description copied from interface: Table
      Compares the specified object with this table for equality. Two tables are equal when their cell views, as returned by Table.cellSet(), are equal.
      Specified by:
      equals in interface Table<R extends @Nullable Object,C extends @Nullable Object,V extends @Nullable Object>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Description copied from interface: Table
      Returns the hash code for this table. The hash code of a table is defined as the hash code of its cell view, as returned by Table.cellSet().
      Specified by:
      hashCode in interface Table<R extends @Nullable Object,C extends @Nullable Object,V extends @Nullable Object>
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Returns the string representation rowMap().toString().
      Overrides:
      toString in class Object