com.google.common.collect
Class ArrayTable<R,C,V>

java.lang.Object
  extended by com.google.common.collect.ArrayTable<R,C,V>
All Implemented Interfaces:
Table<R,C,V>, Serializable

@Beta
public final class ArrayTable<R,C,V>
extends Object
implements Table<R,C,V>, Serializable

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.

See the Guava User Guide article on Table.

Since:
10.0
Author:
Jared Levy
See Also:
Serialized Form

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
<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. 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

create

public static <R,C,V> ArrayTable<R,C,V> create(Iterable<? extends R> rowKeys,
                                               Iterable<? extends C> columnKeys)
Creates an empty ArrayTable.

Parameters:
rowKeys - row keys that may be stored in the generated table
columnKeys - column keys that may be stored in the generated table
Throws:
NullPointerException - if any of the provided keys is null
IllegalArgumentException - if rowKeys or columnKeys contains duplicates or is empty

create

public static <R,C,V> ArrayTable<R,C,V> create(Table<R,C,V> table)
Creates an 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.

Throws:
NullPointerException - if table has a null key
IllegalArgumentException - if the provided table is empty

create

public 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.


rowKeyList

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.


columnKeyList

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.


at

public V at(int rowIndex,
            int columnIndex)
Returns the value corresponding to the specified row and column indices. The same value is returned by get(rowKeyList().get(rowIndex), columnKeyList().get(columnIndex)), but this method runs more quickly.

Parameters:
rowIndex - position of the row key in rowKeyList()
columnIndex - position of the row key in columnKeyList()
Returns:
the value with the specified row and column
Throws:
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

set

public V set(int rowIndex,
             int columnIndex,
             @Nullable
             V value)
Associates 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.

Parameters:
rowIndex - position of the row key in rowKeyList()
columnIndex - position of the row key in columnKeyList()
value - value to store in the table
Returns:
the previous value with the specified row and column
Throws:
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

toArray

public V[][] toArray(Class<V> valueClass)
Returns a two-dimensional array with the table contents. The row and column indices correspond to the positions of the row and column in the iterables provided during table construction. If the table lacks a mapping for a given row and column, the corresponding array element is null.

Subsequent table changes will not modify the array, and vice versa.

Parameters:
valueClass - class of values stored in the returned array

clear

@Deprecated
public void clear()
Deprecated. Use eraseAll()

Not supported. Use eraseAll() instead.

Specified by:
clear in interface Table<R,C,V>
Throws:
UnsupportedOperationException - always

eraseAll

public void eraseAll()
Associates the value null with every pair of allowed row and column keys.


contains

public boolean contains(@Nullable
                        Object rowKey,
                        @Nullable
                        Object columnKey)
Returns true if the provided keys are among the keys provided when the table was constructed.

Specified by:
contains in interface Table<R,C,V>
Parameters:
rowKey - key of row to search for
columnKey - key of column to search for

containsColumn

public boolean containsColumn(@Nullable
                              Object columnKey)
Returns true if the provided column key is among the column keys provided when the table was constructed.

Specified by:
containsColumn in interface Table<R,C,V>
Parameters:
columnKey - key of column to search for

containsRow

public boolean containsRow(@Nullable
                           Object rowKey)
Returns true if the provided row key is among the row keys provided when the table was constructed.

Specified by:
containsRow in interface Table<R,C,V>
Parameters:
rowKey - key of row to search for

containsValue

public boolean containsValue(@Nullable
                             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>
Parameters:
value - value to search for

get

public V get(@Nullable
             Object rowKey,
             @Nullable
             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>
Parameters:
rowKey - key of row to search for
columnKey - key of column to search for

isEmpty

public boolean isEmpty()
Always returns false.

Specified by:
isEmpty in interface Table<R,C,V>

put

public V put(R rowKey,
             C columnKey,
             @Nullable
             V value)
Associates the specified value with the specified keys. If the table already contained a mapping for those keys, the old value is replaced with the specified value.

Specified by:
put in interface Table<R,C,V>
Parameters:
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
Returns:
the value previously associated with the keys, or null if no mapping existed for the keys
Throws:
IllegalArgumentException - if rowKey is not in rowKeySet() or columnKey is not in columnKeySet().

putAll

public void putAll(Table<? extends R,? extends C,? extends V> table)
Copies all mappings from the specified table to this table. The effect is equivalent to calling 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.

Specified by:
putAll in interface Table<R,C,V>
Parameters:
table - the table to add to this table
Throws:
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()

remove

@Deprecated
public V remove(Object rowKey,
                           Object columnKey)
Deprecated. Use erase(java.lang.Object, java.lang.Object)

Not supported. Use erase(java.lang.Object, java.lang.Object) instead.

Specified by:
remove in interface Table<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
Throws:
UnsupportedOperationException - always

erase

public V erase(@Nullable
               Object rowKey,
               @Nullable
               Object columnKey)
Associates the value 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.

Parameters:
rowKey - row key of mapping to be erased
columnKey - column key of mapping to be erased
Returns:
the value previously associated with the keys, or null if no mapping existed for the keys

size

public int size()
Description copied from interface: Table
Returns the number of row key / column key / value mappings in the table.

Specified by:
size in interface Table<R,C,V>

equals

public boolean equals(@Nullable
                      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 Object

hashCode

public int hashCode()
Description copied from interface: Table
Returns the hash code for this table. The hash code of a table is defined as the hash code of its cell view, as returned by Table.cellSet().

Specified by:
hashCode in interface Table<R,C,V>
Overrides:
hashCode in class Object

toString

public String toString()
Returns the string representation rowMap().toString().

Overrides:
toString in class Object

cellSet

public Set<Table.Cell<R,C,V>> cellSet()
Returns an unmodifiable set of all row key / column key / value triplets. Changes to the table will update the returned set.

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.

Specified by:
cellSet in interface Table<R,C,V>
Returns:
set of table cells consisting of row key / column key / value triplets

column

public Map<R,V> column(C columnKey)
Returns a view of all mappings that have the given column key. If the column key isn't in 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.

Specified by:
column in interface Table<R,C,V>
Parameters:
columnKey - key of column to search for in the table
Returns:
the corresponding map from row keys to values

columnKeySet

public ImmutableSet<C> columnKeySet()
Returns an immutable set of the valid column keys, including those that are associated with null values only.

Specified by:
columnKeySet in interface Table<R,C,V>
Returns:
immutable set of column keys

columnMap

public Map<C,Map<R,V>> columnMap()
Description copied from interface: Table
Returns a view that associates each column key with the corresponding map from row keys to values. Changes to the returned map will update this table. The returned map does not support 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().

Specified by:
columnMap in interface Table<R,C,V>
Returns:
a map view from each column key to a secondary map from row keys to values

row

public Map<C,V> row(R rowKey)
Returns a view of all mappings that have the given row key. If the row key isn't in 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.

Specified by:
row in interface Table<R,C,V>
Parameters:
rowKey - key of row to search for in the table
Returns:
the corresponding map from column keys to values

rowKeySet

public ImmutableSet<R> rowKeySet()
Returns an immutable set of the valid row keys, including those that are associated with null values only.

Specified by:
rowKeySet in interface Table<R,C,V>
Returns:
immutable set of row keys

rowMap

public Map<R,Map<C,V>> rowMap()
Description copied from interface: Table
Returns a view that associates each row key with the corresponding map from column keys to values. Changes to the returned map will update this table. The returned map does not support 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().

Specified by:
rowMap in interface Table<R,C,V>
Returns:
a map view from each row key to a secondary map from column keys to values

values

public Collection<V> values()
Returns an unmodifiable collection of all values, which may contain duplicates. Changes to the table will update the returned collection.

The returned collection's iterator traverses the values of the first row key, the values of the second row key, and so on.

Specified by:
values in interface Table<R,C,V>
Returns:
collection of values


Copyright © 2010-2012. All Rights Reserved.