How to Iterate the Table Using the Index.Iterator
There are several methods to programmatically access data from a table. The simplest is to retrieve one cell you may call table.getColumn("Name").get(i)
, where i
is the row's position in the table (from 0 to size - 1). However, using the .get()
method on a column is not the most efficient way to get many rows, because each call to get() performs an O(lg n)
lookup on the table's Index structure. If you would like to iterate through all of the rows in a table, it is much more efficient to iterate through the index structure and access the table's underlying column source, which is referenced by Index key (rather than the position within the table):
ColumnSource columnSource = table.getColumnSource("Name");
ColumnSource intColumnSource = table.getColumnSource("IntCol");
for (Index.Iterator it = table.getIndex().iterator(); it.hasNext();){
long rowKey = it.nextLong();
object value = columnSource.get(rowKey);
int intValue = columnSource.getInt(rowKey);
}
We will unpack the syntax below and explain the steps in the process.
ColumnSource columnSource = table.getColumnSource("Name");
ColumnSource intColumnSource = table.getColumnSource("IntCol");
These arguments save the column source into a variable. You can getColumnSource(s)
by "Name" or using getColumnSourceMap()
to get a Map from name to ColumnSource for all the columns in the table.
for (Index.Iterator it=table.getIndex().iterator(); it.hasNext(); ) {
While getting the ColumnSources gets the keys for the table, the index actually describes what is contained in the rows of the table. Our next step is a "for loop", or standard Java PrimitiveIterator.OfLong
. This first provides a list of keys that are valid within the table, and the iterator allows you to go through those keys from start to finish. There are three parts here: the initialization (Index.Iterator it=table.getIndex().iterator();
), the check (it.hasNext();
), and the post operator - in this case, nothing because we are updating the value inside the loop, and after we execute the loop the it.hasNext()
operation in the check will appropriately return true
if there is another item to process, or false
if not.
long rowKey = it.nextLong();
To retrieve values using a ColumnSource, we need a row key. Using the nextLong()
call rather than next()
prevents boxing, which would also occur if a range-based for loop were used. When programming in Java, it is always best to avoid boxing (converting a primitive value into an object) because it requires more memory.
object value = columnSource.get(rowKey);
int intValue = columnSource.getInt(rowKey);
With the row key, we can "get" values for particular rows by asking the the different column sources to return the value for that key. get
is the most generic and gives you an object. However, for primitive types, it is more efficient to call getByte
, getInt
, getShort
, getLong
, getChar
, getFloat
, or getDouble
to avoid boxing.
In this example, we simply retrieve the values from the Deephaven table, but you should insert your own business logic into the loop.