@Beta @GwtCompatible(emulated=true) public final class ArrayTable<R,C extends Object,V> implements Serializable
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. 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.
Table.Cell <R,C,V> | Modifier and Type | Method and Description |
|---|---|
V |
at(int rowIndex, int columnIndex)
Returns the value corresponding to the specified row and column indices.
|
Set |
cellSet()
Returns an unmodifiable set of all row key / column key / value triplets.
|
void |
clear()
Deprecated.
Use
eraseAll()
|
Map |
column(C columnKey)
Returns a view of all mappings that have the given column key.
|
ImmutableList |
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 |
columnKeySet()
Returns an immutable set of the valid column keys, including those that are associated with null values only.
|
Map |
columnMap()
Returns a view that associates each column key with the corresponding map from row keys to values.
|
boolean |
contains(Object
Returns
true if the provided keys are among the keys provided when the table was constructed.
|
boolean |
containsColumn(Object
Returns
true if the provided column key is among the column keys provided when the table was constructed.
|
boolean |
containsRow(Object
Returns
true if the provided row key is among the row keys provided when the table was constructed.
|
boolean |
containsValue(Object
Returns
true if the table contains a mapping with the specified value.
|
static <R |
create(Iterable
Creates an empty
ArrayTable.
|
static <R |
create(Table
Creates an
ArrayTable with the mappings in the provided table.
|
boolean |
equals(Object
Compares the specified object with this table for equality.
|
V |
erase(Object
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(Object
Returns the value corresponding to the given row and column keys, or
null if no such mapping exists.
|
int |
hashCode()
Returns the hash code for this table.
|
boolean |
isEmpty()
Always returns
false.
|
V |
put(R rowKey, C columnKey, V value)
Associates the specified value with the specified keys.
|
void |
putAll(Table
Copies all mappings from the specified table to this table.
|
V |
remove(Object
Deprecated.
|
Map |
row(R rowKey)
Returns a view of all mappings that have the given row key.
|
ImmutableList |
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 |
rowKeySet()
Returns an immutable set of the valid row keys, including those that are associated with null values only.
|
Map |
rowMap()
Returns a view that associates each row key with the corresponding map from column keys to values.
|
V |
set(int rowIndex, int columnIndex, 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
Returns a two-dimensional array with the table contents.
|
String |
toString()
Returns the string representation
rowMap().toString().
|
Collection |
values()
Returns an unmodifiable collection of all values, which may contain duplicates.
|
public static <R,C ,V> ArrayTable <R ,C ,V> create(Iterable <? extends R> rowKeys, Iterable <? extends C> columnKeys)
ArrayTable.
rowKeys - row keys that may be stored in the generated table
columnKeys - column keys that may be stored in the generated table
NullPointerException - if any of the provided keys is null
IllegalArgumentException - if
rowKeys or
columnKeys contains duplicates or is empty
public static <R,C ,V> ArrayTable <R ,C ,V> create(Table <R ,C ,V> table)
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.
NullPointerException - if
table has a null key
IllegalArgumentException - if the provided table is empty
public ImmutableList<R> rowKeyList()
public ImmutableList<C> columnKeyList()
public V at(int rowIndex, int columnIndex)
get(rowKeyList().get(rowIndex), columnKeyList().get(columnIndex)), but this method runs more quickly.
rowIndex - position of the row key in
rowKeyList()
columnIndex - position of the row key in
columnKeyList()
IndexOutOfBoundsException - if either index is negative,
rowIndex is greater then or equal to the number of allowed row keys, or
columnIndex is greater then or equal to the number of allowed column keys
public V set(int rowIndex, int columnIndex, V value)
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.
rowIndex - position of the row key in
rowKeyList()
columnIndex - position of the row key in
columnKeyList()
value - value to store in the table
IndexOutOfBoundsException - if either index is negative,
rowIndex is greater then or equal to the number of allowed row keys, or
columnIndex is greater then or equal to the number of allowed column keys
@GwtIncompatible(value="reflection") public V[][] toArray(Class<V> valueClass)
Subsequent table changes will not modify the array, and vice versa.
valueClass - class of values stored in the returned array
@Deprecated public void clear()
eraseAll()
eraseAll() instead.
public void eraseAll()
null with every pair of allowed row and column keys.
public boolean contains(ObjectrowKey, Object columnKey)
true if the provided keys are among the keys provided when the table was constructed.
public boolean containsColumn(ObjectcolumnKey)
true if the provided column key is among the column keys provided when the table was constructed.
containsColumn in interface
Table<R,C,V>
columnKey - key of column to search for
public boolean containsRow(ObjectrowKey)
true if the provided row key is among the row keys provided when the table was constructed.
containsRow in interface
Table<R,C,V>
rowKey - key of row to search for
public boolean containsValue(Objectvalue)
Table
true if the table contains a mapping with the specified value.
containsValue in interface
Table<R,C,V>
value - value to search for
public V get(ObjectrowKey, Object columnKey)
Table
null if no such mapping exists.
public boolean isEmpty()
false.
public V put(R rowKey, C columnKey, V value)
put in interface
Table<R,C,V>
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
null if no mapping existed for the keys
IllegalArgumentException - if
rowKey is not in
rowKeySet() or
columnKey is not in
columnKeySet().
public void putAll(Table<? extends R ,? extends C ,? extends V> table)
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.
putAll in interface
Table<R,C,V>
table - the table to add to this table
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()
@Deprecated public V remove(ObjectrowKey, Object columnKey)
erase(java.lang.Object, java.lang.Object)
erase(java.lang.Object, java.lang.Object) instead.
public V erase(ObjectrowKey, Object columnKey)
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.
rowKey - row key of mapping to be erased
columnKey - column key of mapping to be erased
null if no mapping existed for the keys
public int size()
Table
public Set<Table .Cell <R ,C ,V>> cellSet()
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.
public Map<R ,V> column(C columnKey)
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.
public ImmutableSet<C> columnKeySet()
columnKeySet in interface
Table<R,C,V>
public Map<C ,Map <R ,V>> columnMap()
Table
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. Those maps may support setValue(), put(), and putAll().
public Map<C ,V> row(R rowKey)
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.
public ImmutableSet<R> rowKeySet()
public Map<R ,Map <C ,V>> rowMap()
Table
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. Those maps may support setValue(), put(), and putAll().
public Collection<V> values()
The returned collection's iterator traverses the values of the first row key, the values of the second row key, and so on.
public boolean equals(Objectobj)
Table
Table.cellSet() , are equal.
public int hashCode()
Table
Table.cellSet() .
public StringtoString()
rowMap().toString().