Class HashBasedTable<R,​C,​V>

  • All Implemented Interfaces:
    Table<R,​C,​V>, java.io.Serializable

    @GwtCompatible(serializable=true)
    public class HashBasedTable<R,​C,​V>
    extends StandardTable<R,​C,​V>
    Implementation of Table using linked hash tables. This guarantees predictable iteration order of the various views.

    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.

    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
    • Constructor Detail

    • Method Detail

      • create

        public static <R,​C,​V> HashBasedTable<R,​C,​V> create()
        Creates an empty HashBasedTable.
      • create

        public static <R,​C,​V> HashBasedTable<R,​C,​V> create​(int expectedRows,
                                                                                   int expectedCellsPerRow)
        Creates an empty HashBasedTable with the specified map sizes.
        Parameters:
        expectedRows - the expected number of distinct row keys
        expectedCellsPerRow - the expected number of column key / value mappings in each row
        Throws:
        java.lang.IllegalArgumentException - if expectedRows or expectedCellsPerRow is negative
      • create

        public static <R,​C,​V> HashBasedTable<R,​C,​V> create​(Table<? extends R,​? extends C,​? extends V> table)
        Creates a HashBasedTable with the same mappings as the specified table.
        Parameters:
        table - the table to copy
        Throws:
        java.lang.NullPointerException - if any of the row keys, column keys, or values in table is null
      • contains

        public boolean contains​(java.lang.Object rowKey,
                                java.lang.Object columnKey)
        Description copied from interface: Table
        Returns true if the table contains a mapping with the specified row and column keys.
        Specified by:
        contains in interface Table<R,​C,​V>
        Overrides:
        contains in class StandardTable<R,​C,​V>
        Parameters:
        rowKey - key of row to search for
        columnKey - key of column to search for
      • containsColumn

        public boolean containsColumn​(java.lang.Object columnKey)
        Description copied from interface: Table
        Returns true if the table contains a mapping with the specified column.
        Specified by:
        containsColumn in interface Table<R,​C,​V>
        Overrides:
        containsColumn in class StandardTable<R,​C,​V>
        Parameters:
        columnKey - key of column to search for
      • containsRow

        public boolean containsRow​(java.lang.Object rowKey)
        Description copied from interface: Table
        Returns true if the table contains a mapping with the specified row key.
        Specified by:
        containsRow in interface Table<R,​C,​V>
        Overrides:
        containsRow in class StandardTable<R,​C,​V>
        Parameters:
        rowKey - key of row to search for
      • containsValue

        public boolean containsValue​(java.lang.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>
        Overrides:
        containsValue in class StandardTable<R,​C,​V>
        Parameters:
        value - value to search for
      • get

        public V get​(java.lang.Object rowKey,
                     java.lang.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>
        Overrides:
        get in class StandardTable<R,​C,​V>
        Parameters:
        rowKey - key of row to search for
        columnKey - key of column to search for
      • equals

        public boolean equals​(java.lang.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,​C,​V>
        Overrides:
        equals in class AbstractTable<R,​C,​V>
      • remove

        public V remove​(java.lang.Object rowKey,
                        java.lang.Object columnKey)
        Description copied from interface: Table
        Removes the mapping, if any, associated with the given keys.
        Specified by:
        remove in interface Table<R,​C,​V>
        Overrides:
        remove in class StandardTable<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