|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.google.common.collect.ArrayTable<R,C,V>
@Beta public final class ArrayTable<R,C,V>
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, 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.
| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from interface com.google.common.collect.Table |
|---|
Table.Cell<R,C,V> |
| Method Summary | ||
|---|---|---|
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
|
create(ArrayTable<R,C,V> table)
Creates an ArrayTable with the same mappings, allowed keys, and
iteration ordering as the provided ArrayTable. |
|
static
|
create(Iterable<? extends R> rowKeys,
Iterable<? extends C> columnKeys)
Creates an empty ArrayTable. |
|
static
|
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. Use erase(java.lang.Object, java.lang.Object) |
|
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. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Method Detail |
|---|
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 table
NullPointerException - if any of the provided keys is null
IllegalArgumentException - 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 key
IllegalArgumentException - 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 keys
public 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 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 keyspublic 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.
clear in interface Table<R,C,V>UnsupportedOperationException - alwayspublic 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.
contains in interface Table<R,C,V>rowKey - key of row to search forcolumnKey - key of column to search for
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 for
public 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 for
public boolean containsValue(@Nullable
Object value)
Tabletrue 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(@Nullable
Object rowKey,
@Nullable
Object columnKey)
Tablenull if no such mapping exists.
get in interface Table<R,C,V>rowKey - key of row to search forcolumnKey - key of column to search forpublic boolean isEmpty()
false.
isEmpty in interface Table<R,C,V>
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 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(Object rowKey,
Object columnKey)
erase(java.lang.Object, java.lang.Object)
erase(java.lang.Object, java.lang.Object) instead.
remove in interface Table<R,C,V>rowKey - row key of mapping to be removedcolumnKey - column key of mapping to be removed
null if
no such value existed
UnsupportedOperationException - always
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 erased
null if
no mapping existed for the keyspublic int size()
Table
size in interface Table<R,C,V>
public boolean equals(@Nullable
Object obj)
TableTable.cellSet(), are equal.
equals in interface Table<R,C,V>equals in class Objectpublic int hashCode()
TableTable.cellSet().
hashCode in interface Table<R,C,V>hashCode in class Objectpublic String toString()
rowMap().toString().
toString in class Objectpublic 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.
cellSet in interface Table<R,C,V>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.
column in interface Table<R,C,V>columnKey - key of column to search for in the table
public ImmutableSet<C> columnKeySet()
columnKeySet in interface Table<R,C,V>public Map<C,Map<R,V>> columnMap()
Tableput() 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().
columnMap in interface Table<R,C,V>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.
row in interface Table<R,C,V>rowKey - key of row to search for in the table
public ImmutableSet<R> rowKeySet()
rowKeySet in interface Table<R,C,V>public Map<R,Map<C,V>> rowMap()
Tableput() 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().
rowMap in interface Table<R,C,V>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.
values in interface Table<R,C,V>
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||