Class ArrayTable<R,C,V>

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

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

    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:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface com.google.common.collect.Table

        Table.Cell<R,C,V>
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      V at(int rowIndex, int columnIndex)
      Returns the value corresponding to the specified row and column indices.
      Set<Table.Cell<R,C,V>> cellSet()
      Returns an unmodifiable set of all row key / column key / value triplets.
      void clear()
      Deprecated.
      Map<R,V> column(C columnKey)
      Returns a view of all mappings that have the given column key.
      ImmutableList<C> columnKeyList()
      Returns, as an immutable list, the column keys provided when the table was constructed, including those that are mapped to null values only.
      ImmutableSet<C> columnKeySet()
      Returns an immutable set of the valid column keys, including those that are associated with null values only.
      Map<C,Map<R,V>> columnMap()
      Returns a view that associates each column key with the corresponding map from row keys to values.
      boolean contains(@Nullable Object rowKey, @Nullable Object columnKey)
      Returns true if the provided keys are among the keys provided when the table was constructed.
      boolean containsColumn(@Nullable Object columnKey)
      Returns true if the provided column key is among the column keys provided when the table was constructed.
      boolean containsRow(@Nullable Object rowKey)
      Returns true if the provided row key is among the row keys provided when the table was constructed.
      boolean containsValue(@Nullable Object value)
      Returns true if the table contains a mapping with the specified value.
      static <R,C,V>
      ArrayTable<R,C,V>
      create(Table<R,C,V> table)
      Creates an ArrayTable with the mappings in the provided table.
      static <R,C,V>
      ArrayTable<R,C,V>
      create(Iterable<? extends R> rowKeys, Iterable<? extends C> columnKeys)
      Creates an ArrayTable filled with null.
      boolean equals(@Nullable Object obj)
      Indicates whether some other object is "equal to" this one.
      V erase(@Nullable Object rowKey, @Nullable Object columnKey)
      Associates the value null with the specified keys, assuming both keys are valid.
      void eraseAll()
      Associates the value null with every pair of allowed row and column keys.
      V get(@Nullable Object rowKey, @Nullable Object columnKey)
      Returns the value corresponding to the given row and column keys, or null if no such mapping exists.
      int hashCode()
      Returns a hash code value for the object.
      boolean isEmpty()
      Returns true if rowKeyList().size == 0 or columnKeyList().size() == 0.
      V put(R rowKey, C columnKey, @Nullable V value)
      Associates the specified value with the specified keys.
      void putAll(Table<? extends R,? extends C,? extends V> table)
      Copies all mappings from the specified table to this table.
      V remove(Object rowKey, Object columnKey)
      Map<C,V> row(R rowKey)
      Returns a view of all mappings that have the given row key.
      ImmutableList<R> rowKeyList()
      Returns, as an immutable list, the row keys provided when the table was constructed, including those that are mapped to null values only.
      ImmutableSet<R> rowKeySet()
      Returns an immutable set of the valid row keys, including those that are associated with null values only.
      Map<R,Map<C,V>> rowMap()
      Returns a view that associates each row key with the corresponding map from column keys to values.
      V set(int rowIndex, int columnIndex, @Nullable V value)
      Associates value with the specified row and column indices.
      int size()
      Returns the number of row key / column key / value mappings in the table.
      V[][] toArray(Class<V> valueClass)
      Returns a two-dimensional array with the table contents.
      String toString()
      Returns the string representation rowMap().toString().
      Collection<V> values()
      Returns an unmodifiable collection of all values, which may contain duplicates.
    • Method Detail

      • 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,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

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

        public ImmutableList<CcolumnKeyList()
        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 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 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 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
      • 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 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 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 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()
      • erase

        @CanIgnoreReturnValue
        public V erase(@Nullable Object rowKey,
                       @Nullable Object columnKey)
        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

        public Set<Table.Cell<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,Vcolumn(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

        public ImmutableSet<CcolumnKeySet()
        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,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,Vrow(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

        public ImmutableSet<RrowKeySet()
        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,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

        public Collection<Vvalues()
        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 class: java.lang.Object
        Indicates whether some other object is "equal to" this one.

        The equals method implements an equivalence relation on non-null object references:

        • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
        • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
        • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
        • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
        • For any non-null reference value x, x.equals(null) should return false.

        The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

        Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

        Specified by:
        equals in interface Table<R,C,V>
        Overrides:
        equals in class Object
        Parameters:
        obj - the reference object with which to compare.
        Returns:
        true if this object is the same as the obj argument; false otherwise.
        See Also:
        Object.hashCode(), HashMap
      • hashCode

        public int hashCode()
        Description copied from class: java.lang.Object
        Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.

        The general contract of hashCode is:

        • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
        • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
        • It is not required that if two objects are unequal according to the Object.equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

        As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (The hashCode may or may not be implemented as some function of an object's memory address at some point in time.)

        Specified by:
        hashCode in interface Table<R,C,V>
        Overrides:
        hashCode in class Object
        Returns:
        a hash code value for this object.
        See Also:
        Object.equals(java.lang.Object), System.identityHashCode(java.lang.Object)
      • toString

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