Top Description Inners Fields Constructors Methods
com.google.common.collect

public final Class ArrayTable<R, C, V>

extends AbstractTable<R, C, @Nullable V>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkElementIndex, .Preconditions.checkNotNull, java.util.Collections.emptyMap

Fixed-size Table implementation backed by a two-dimensional array.

Warning

ArrayTable is rarely the Table implementation you want. First, it requires that the complete universe of rows and columns be specified at construction time. Second, it is always backed by an array large enough to hold a value for every possible combination of row and column keys. (This is rarely optimal unless the table is extremely dense.) Finally, every possible combination of row and column keys is always considered to have a value associated with it: It is not possible to "remove" a value, only to replace it with null, which will still appear when iterating over the table's contents in a foreach loop or a call to a null-hostile method like ImmutableTable#copyOf. For alternatives, please see the wiki.

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 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, set, toArray, 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.

Author
Jared Levy
Since
10.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private abstract static class
private class
private class
private class
private class

Field Summary

Modifier and TypeField and Description
private final @Nullable V[][]
private final ImmutableMap<C, Integer>
private final ImmutableList<C>
private transient ArrayTable<R, C, V>.ColumnMap
private final ImmutableMap<R, Integer>
private final ImmutableList<R>
private transient ArrayTable<R, C, V>.RowMap
private static final long

Constructor Summary

AccessConstructor and Description
private
ArrayTable(Iterable<? extends R> rowKeys, Iterable<? extends C> columnKeys)

private
ArrayTable(Table<R, C, ? extends @Nullable V> table)

private
ArrayTable(ArrayTable<R, C, V> table)

Method Summary

Modifier and TypeMethod and Description
public V

Returns:

the value with the specified row and column
at
(int
position of the row key in rowKeyList()
rowIndex
,
int
position of the row key in columnKeyList()
columnIndex
)

Returns the value corresponding to the specified row and column indices.

pack-priv Iterator<Table.Cell<R, C, @Nullable V>>
public Set<Table.Cell<R, C, @Nullable V>>

Returns:

set of table cells consisting of row key / column key / value triplets
cellSet
()

Overrides com.google.common.collect.AbstractTable.cellSet.

Implements com.google.common.collect.Table.cellSet.

Returns an unmodifiable set of all row key / column key / value triplets.
pack-priv Spliterator<Table.Cell<R, C, @Nullable V>>
public void
public Map<R, @Nullable V>

Returns:

the corresponding map from row keys to values
column
(C
key of column to search for in the table
columnKey
)

Implements com.google.common.collect.Table.column.

Returns a view of all mappings that have the given column key.
public 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.

public ImmutableSet<C>

Returns:

immutable set of column keys
columnKeySet
()

Overrides com.google.common.collect.AbstractTable.columnKeySet.

Implements com.google.common.collect.Table.columnKeySet.

Returns an immutable set of the valid column keys, including those that are associated with null values only.
public Map<C, Map<R, @Nullable V>>
columnMap()

Implements com.google.common.collect.Table.columnMap.

Returns a view that associates each column key with the corresponding map from row keys to values.
public boolean
contains(Object
key of row to search for
rowKey
,
Object
key of column to search for
columnKey
)

Overrides com.google.common.collect.AbstractTable.contains.

Implements com.google.common.collect.Table.contains.

Returns true if the provided keys are among the keys provided when the table was constructed.
public boolean
containsColumn(Object
key of column to search for
columnKey
)

Overrides com.google.common.collect.AbstractTable.containsColumn.

Implements com.google.common.collect.Table.containsColumn.

Returns true if the provided column key is among the column keys provided when the table was constructed.
public boolean
containsRow(Object
key of row to search for
rowKey
)

Overrides com.google.common.collect.AbstractTable.containsRow.

Implements com.google.common.collect.Table.containsRow.

Returns true if the provided row key is among the row keys provided when the table was constructed.
public boolean
containsValue(Object
value to search for
value
)

Overrides com.google.common.collect.AbstractTable.containsValue.

Implements com.google.common.collect.Table.containsValue.

Returns true if the table contains a mapping with the specified value.
public static <R, C, V> ArrayTable<R, C, V>
create(Iterable<? extends R>
row keys that may be stored in the generated table
rowKeys
,
Iterable<? extends C>
column keys that may be stored in the generated table
columnKeys
)

Creates an ArrayTable filled with null.

public static <R, C, V> ArrayTable<R, C, V>
create(Table<R, C, ? extends @Nullable V> table)

Creates an ArrayTable with the mappings in the provided table.

public V

Returns:

the value previously associated with the keys, or null if no mapping existed for the keys
erase
(Object
row key of mapping to be erased
rowKey
,
Object
column key of mapping to be erased
columnKey
)

Associates the value null with the specified keys, assuming both keys are valid.

public void
eraseAll()

Associates the value null with every pair of allowed row and column keys.

public V
get(Object
key of row to search for
rowKey
,
Object
key of column to search for
columnKey
)

Overrides com.google.common.collect.AbstractTable.get.

Implements com.google.common.collect.Table.get.

Returns the value corresponding to the given row and column keys, or null if no such mapping exists.
private Table.Cell<R, C, @Nullable V>
getCell(final int index)

private V
getValue(int index)

public boolean
isEmpty()

Overrides com.google.common.collect.AbstractTable.isEmpty.

Implements com.google.common.collect.Table.isEmpty.

Returns true if rowKeyList().size == 0 or columnKeyList().size() == 0.
public V
put(R
row key that the value should be associated with
rowKey
,
C
column key that the value should be associated with
columnKey
,
V
value to be associated with the specified keys
value
)

Overrides com.google.common.collect.AbstractTable.put.

Implements com.google.common.collect.Table.put.

Associates the specified value with the specified keys.
public void
putAll(Table<? extends R, ? extends C, ? extends @Nullable V>
the table to add to this table
table
)

Overrides com.google.common.collect.AbstractTable.putAll.

Implements com.google.common.collect.Table.putAll.

Copies all mappings from the specified table to this table.
public V
remove(Object
row key of mapping to be removed
rowKey
,
Object
column key of mapping to be removed
columnKey
)

Overrides com.google.common.collect.AbstractTable.remove.

Implements com.google.common.collect.Table.remove.

Deprecated Use erase
Not supported.
public Map<C, @Nullable V>

Returns:

the corresponding map from column keys to values
row
(R
key of row to search for in the table
rowKey
)

Implements com.google.common.collect.Table.row.

Returns a view of all mappings that have the given row key.
public 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.

public ImmutableSet<R>

Returns:

immutable set of row keys
rowKeySet
()

Overrides com.google.common.collect.AbstractTable.rowKeySet.

Implements com.google.common.collect.Table.rowKeySet.

Returns an immutable set of the valid row keys, including those that are associated with null values only.
public Map<R, Map<C, @Nullable V>>
rowMap()

Implements com.google.common.collect.Table.rowMap.

Returns a view that associates each row key with the corresponding map from column keys to values.
public V

Returns:

the previous value with the specified row and column
set
(int
position of the row key in rowKeyList()
rowIndex
,
int
position of the row key in columnKeyList()
columnIndex
,
V
value to store in the table
value
)

Associates value with the specified row and column indices.

public int
size()

Implements com.google.common.collect.Table.size.

Returns the number of row key / column key / value mappings in the table.
public @Nullable V[][]
toArray(Class<V>
class of values stored in the returned array
valueClass
)

Returns a two-dimensional array with the table contents.

public Collection<@Nullable V>

Returns:

collection of values
values
()

Overrides com.google.common.collect.AbstractTable.values.

Implements com.google.common.collect.Table.values.

Returns an unmodifiable collection of all values, which may contain duplicates.
pack-priv Iterator<@Nullable V>
pack-priv Spliterator<@Nullable V>
Inherited from com.google.common.collect.AbstractTable:
createCellSetcreateValuesequalshashCodetoString