@Beta @GwtCompatible(emulated=true) public final class ArrayTable<R,C,V> extends Object implements Table<R,C,V>, 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.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
.
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<Table.Cell<R,C,V>> |
cellSet()
Returns an unmodifiable set of all row key / column key / value
triplets.
|
void |
clear()
Deprecated.
Use
eraseAll() |
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(Object rowKey,
Object columnKey)
Returns
true if the provided keys are among the keys provided when
the table was constructed. |
boolean |
containsColumn(Object columnKey)
Returns
true if the provided column key is among the column keys
provided when the table was constructed. |
boolean |
containsRow(Object rowKey)
Returns
true if the provided row key is among the row keys
provided when the table was constructed. |
boolean |
containsValue(Object value)
Returns
true if the table contains a mapping with the specified
value. |
static <R,C,V> ArrayTable<R,C,V> |
create(ArrayTable<R,C,V> table)
Creates an
ArrayTable with the same mappings, allowed keys, and
iteration ordering as the provided ArrayTable . |
static <R,C,V> ArrayTable<R,C,V> |
create(Iterable<? extends R> rowKeys,
Iterable<? extends C> columnKeys)
Creates an empty
ArrayTable . |
static <R,C,V> ArrayTable<R,C,V> |
create(Table<R,C,V> table)
Creates an
ArrayTable with the mappings in the provided table. |
boolean |
equals(Object obj)
Compares the specified object with this table for equality.
|
V |
erase(Object rowKey,
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(Object rowKey,
Object columnKey)
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<? extends R,? extends C,? extends V> table)
Copies all mappings from the specified table to this table.
|
V |
remove(Object rowKey,
Object columnKey)
Deprecated.
|
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,
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.
|
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 tablecolumnKeys
- column keys that may be stored in the generated tableNullPointerException
- if any of the provided keys is nullIllegalArgumentException
- if rowKeys
or columnKeys
contains duplicates or is emptypublic 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 keyIllegalArgumentException
- if the provided table is emptypublic static <R,C,V> ArrayTable<R,C,V> create(ArrayTable<R,C,V> table)
ArrayTable
with the same mappings, allowed keys, and
iteration ordering as the provided ArrayTable
.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 keyspublic V set(int rowIndex, int columnIndex, @Nullable 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 tableIndexOutOfBoundsException
- 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(@Nullable Object rowKey, @Nullable Object columnKey)
true
if the provided keys are among the keys provided when
the table was constructed.public boolean containsColumn(@Nullable Object columnKey)
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 forpublic boolean containsRow(@Nullable Object rowKey)
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 forpublic boolean containsValue(@Nullable Object value)
Table
true
if the table contains a mapping with the specified
value.containsValue
in interface Table<R,C,V>
value
- value to search forpublic V get(@Nullable Object rowKey, @Nullable Object columnKey)
Table
null
if no such mapping exists.public boolean isEmpty()
false
.public V put(R rowKey, C columnKey, @Nullable V value)
put
in interface Table<R,C,V>
rowKey
- row key that the value should be associated withcolumnKey
- column key that the value should be associated withvalue
- value to be associated with the specified keysnull
if
no mapping existed for the keysIllegalArgumentException
- 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 tableNullPointerException
- if table
has a null keyIllegalArgumentException
- if any of the provided table's row keys or
column keys is not in rowKeySet()
or columnKeySet()
@Deprecated public V remove(Object rowKey, Object columnKey)
erase(java.lang.Object, java.lang.Object)
erase(java.lang.Object, java.lang.Object)
instead.public V erase(@Nullable Object rowKey, @Nullable 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 erasedcolumnKey
- column key of mapping to be erasednull
if
no mapping existed for the keyspublic int size()
Table
public boolean equals(@Nullable Object obj)
Table
Table.cellSet()
, are equal.public int hashCode()
Table
Table.cellSet()
.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.column(C)
. 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.row(R)
. 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.
Copyright © 2010-2013. All Rights Reserved.