public class Table
extends java.lang.Object
Generic class for handling tabular data, typically from a CSV, TSV, or other sort of spreadsheet file.
CSV files are comma separated values, often with the data in quotes. TSV files use tabs as separators, and usually don't bother with the quotes.
File names should end with .csv if they're comma separated.
A rough "spec" for CSV can be found here.
Modifier and Type | Field and Description |
---|---|
static int |
CATEGORY |
static int |
DOUBLE |
static int |
FLOAT |
static int |
INT |
static int |
LONG |
static int |
STRING |
Constructor and Description |
---|
Table()
Creates a new, empty table.
|
Table(java.io.File file) |
Table(java.io.File file,
java.lang.String options)
version that uses a File object; future releases (or data types)
may include additional optimizations here
|
Table(java.io.InputStream input) |
Table(java.io.InputStream input,
java.lang.String options)
Read the table from a stream.
|
Table(java.lang.Iterable<TableRow> rows) |
Table(java.sql.ResultSet rs) |
Modifier and Type | Method and Description |
---|---|
void |
addColumn() |
void |
addColumn(java.lang.String title) |
void |
addColumn(java.lang.String title,
int type) |
TableRow |
addRow() |
TableRow |
addRow(java.lang.Object[] columnData) |
TableRow |
addRow(TableRow source) |
void |
addRows(Table source) |
int |
checkColumnIndex(java.lang.String title)
Same as getColumnIndex(), but creates the column if it doesn't exist.
|
void |
clearRows() |
Table |
copy()
Make a copy of the current table
|
static java.lang.String |
extensionOptions(boolean loading,
java.lang.String filename,
java.lang.String options) |
TableRow |
findRow(java.lang.String value,
int column) |
TableRow |
findRow(java.lang.String value,
java.lang.String columnName) |
int |
findRowIndex(java.lang.String value,
int column)
Return the row that contains the first String that matches.
|
int |
findRowIndex(java.lang.String value,
java.lang.String columnName)
Return the row that contains the first String that matches.
|
int[] |
findRowIndices(java.lang.String value,
int column)
Return a list of rows that contain the String passed in.
|
int[] |
findRowIndices(java.lang.String value,
java.lang.String columnName)
Return a list of rows that contain the String passed in.
|
java.util.Iterator<TableRow> |
findRowIterator(java.lang.String value,
int column) |
java.util.Iterator<TableRow> |
findRowIterator(java.lang.String value,
java.lang.String columnName) |
java.lang.Iterable<TableRow> |
findRows(java.lang.String value,
int column) |
java.lang.Iterable<TableRow> |
findRows(java.lang.String value,
java.lang.String columnName) |
int |
getColumnCount() |
int |
getColumnIndex(java.lang.String columnName) |
java.lang.String |
getColumnTitle(int col) |
java.lang.String[] |
getColumnTitles() |
int |
getColumnType(int column)
Returns one of Table.STRING, Table.INT, etc...
|
int |
getColumnType(java.lang.String columnName) |
int[] |
getColumnTypes() |
double |
getDouble(int row,
int column) |
double |
getDouble(int row,
java.lang.String columnName) |
double[] |
getDoubleColumn(int col) |
double[] |
getDoubleColumn(java.lang.String name) |
double[] |
getDoubleRow(int row) |
float |
getFloat(int row,
int column)
Get a float value from the specified row and column.
|
float |
getFloat(int row,
java.lang.String columnName) |
float[] |
getFloatColumn(int col) |
float[] |
getFloatColumn(java.lang.String name) |
FloatDict |
getFloatDict(int keyColumn,
int valueColumn) |
FloatDict |
getFloatDict(java.lang.String keyColumnName,
java.lang.String valueColumnName) |
FloatList |
getFloatList(int column) |
FloatList |
getFloatList(java.lang.String columnName) |
float[] |
getFloatRow(int row) |
int |
getInt(int row,
int column) |
int |
getInt(int row,
java.lang.String columnName) |
int[] |
getIntColumn(int col) |
int[] |
getIntColumn(java.lang.String name) |
IntDict |
getIntDict(int keyColumn,
int valueColumn) |
IntDict |
getIntDict(java.lang.String keyColumnName,
java.lang.String valueColumnName) |
IntList |
getIntList(int column) |
IntList |
getIntList(java.lang.String columnName) |
int[] |
getIntRow(int row) |
long |
getLong(int row,
int column) |
long |
getLong(int row,
java.lang.String columnName) |
long[] |
getLongColumn(int col) |
long[] |
getLongColumn(java.lang.String name) |
long[] |
getLongRow(int row) |
IntDict |
getOrder(int column) |
IntDict |
getOrder(java.lang.String columnName) |
TableRow |
getRow(int row) |
int |
getRowCount() |
java.util.Map<java.lang.String,TableRow> |
getRowMap(int column)
Return a mapping that connects the entry from a column back to the row
from which it came.
|
java.util.Map<java.lang.String,TableRow> |
getRowMap(java.lang.String columnName) |
java.lang.String |
getString(int row,
int column)
Get a String value from the table.
|
java.lang.String |
getString(int row,
java.lang.String columnName) |
java.lang.String[] |
getStringColumn(int column) |
java.lang.String[] |
getStringColumn(java.lang.String columnName) |
StringDict |
getStringDict(int keyColumn,
int valueColumn) |
StringDict |
getStringDict(java.lang.String keyColumnName,
java.lang.String valueColumnName) |
StringList |
getStringList(int column) |
StringList |
getStringList(java.lang.String columnName) |
java.lang.String[] |
getStringRow(int row) |
IntDict |
getTally(int column) |
IntDict |
getTally(java.lang.String columnName) |
java.lang.String[] |
getUnique(int column) |
java.lang.String[] |
getUnique(java.lang.String columnName) |
boolean |
hasColumnTitles() |
void |
insertColumn(int index) |
void |
insertColumn(int index,
java.lang.String title) |
void |
insertColumn(int index,
java.lang.String title,
int type) |
void |
insertRow(int insert,
java.lang.Object[] columnData) |
int |
lastRowIndex() |
TableRow |
matchRow(java.lang.String regexp,
int column) |
TableRow |
matchRow(java.lang.String regexp,
java.lang.String columnName) |
int |
matchRowIndex(java.lang.String regexp,
int column)
Return the row that contains the first String that matches.
|
int |
matchRowIndex(java.lang.String what,
java.lang.String columnName)
Return the row that contains the first String that matches.
|
int[] |
matchRowIndices(java.lang.String regexp,
int column)
Return a list of rows that contain the String passed in.
|
int[] |
matchRowIndices(java.lang.String what,
java.lang.String columnName)
Return a list of rows that match the regex passed in.
|
java.util.Iterator<TableRow> |
matchRowIterator(java.lang.String value,
int column) |
java.util.Iterator<TableRow> |
matchRowIterator(java.lang.String value,
java.lang.String columnName) |
java.lang.Iterable<TableRow> |
matchRows(java.lang.String regexp,
int column) |
java.lang.Iterable<TableRow> |
matchRows(java.lang.String regexp,
java.lang.String columnName) |
void |
parseInto(java.lang.Object enclosingObject,
java.lang.String fieldName)
incomplete, do not use
|
void |
print() |
void |
removeColumn(int column) |
void |
removeColumn(java.lang.String columnName) |
void |
removeRow(int row) |
java.lang.String[] |
removeTitleRow()
Deprecated.
|
void |
removeTokens(java.lang.String tokens)
Remove any of the specified characters from the entire table.
|
void |
removeTokens(java.lang.String tokens,
int column)
Removed any of the specified characters from a column.
|
void |
removeTokens(java.lang.String tokens,
java.lang.String columnName) |
void |
replace(java.lang.String orig,
java.lang.String replacement)
Replace a String with another.
|
void |
replace(java.lang.String orig,
java.lang.String replacement,
int col) |
void |
replace(java.lang.String orig,
java.lang.String replacement,
java.lang.String colName) |
void |
replaceAll(java.lang.String regex,
java.lang.String replacement) |
void |
replaceAll(java.lang.String regex,
java.lang.String replacement,
int column) |
void |
replaceAll(java.lang.String regex,
java.lang.String replacement,
java.lang.String columnName)
Run String.replaceAll() on all entries in a column.
|
java.lang.Iterable<TableRow> |
rows()
Note that this one iterator instance is shared by any calls to iterate
the rows of this table.
|
java.lang.Iterable<TableRow> |
rows(int[] indices) |
boolean |
save(java.io.File file,
java.lang.String options) |
boolean |
save(java.io.OutputStream output,
java.lang.String options) |
void |
setColumnCount(int newCount)
Change the number of columns in this table.
|
void |
setColumnTitle(int column,
java.lang.String title) |
void |
setColumnTitles(java.lang.String[] titles) |
void |
setColumnType(int column,
int newType)
Sets the column type.
|
void |
setColumnType(int column,
java.lang.String columnType)
Set the data type for a column so that using it is more efficient.
|
void |
setColumnType(java.lang.String columnName,
int newType) |
void |
setColumnType(java.lang.String columnName,
java.lang.String columnType) |
void |
setColumnTypes(int[] types) |
void |
setColumnTypes(Table dictionary)
Set the titles (and if a second column is present) the data types for
this table based on a file loaded separately.
|
void |
setDouble(int row,
int column,
double value) |
void |
setDouble(int row,
java.lang.String columnName,
double value) |
void |
setFloat(int row,
int column,
float value) |
void |
setFloat(int row,
java.lang.String columnName,
float value) |
void |
setInt(int row,
int column,
int value) |
void |
setInt(int row,
java.lang.String columnName,
int value) |
void |
setLong(int row,
int column,
long value) |
void |
setLong(int row,
java.lang.String columnName,
long value) |
void |
setMissingDouble(double value) |
void |
setMissingFloat(float value) |
void |
setMissingInt(int value) |
void |
setMissingLong(long value) |
void |
setMissingString(java.lang.String value)
Treat entries with this string as "missing".
|
void |
setRow(int row,
java.lang.Object[] pieces) |
TableRow |
setRow(int row,
TableRow source) |
void |
setRowCount(int newCount) |
void |
setString(int row,
int column,
java.lang.String value) |
void |
setString(int row,
java.lang.String columnName,
java.lang.String value) |
void |
setTableType(java.lang.String type)
Set the entire table to a specific data type.
|
void |
sort(int column) |
void |
sort(java.lang.String columnName)
Sorts (orders) a table based on the values in a column.
|
void |
sortReverse(int column) |
void |
sortReverse(java.lang.String columnName) |
void |
trim() |
void |
trim(int column) |
void |
trim(java.lang.String columnName) |
Table |
typedParse(java.io.InputStream input,
java.lang.String options) |
void |
write(java.io.PrintWriter writer) |
public static final int STRING
public static final int INT
public static final int LONG
public static final int FLOAT
public static final int DOUBLE
public static final int CATEGORY
public Table()
public Table(java.io.File file) throws java.io.IOException
java.io.IOException
public Table(java.io.File file, java.lang.String options) throws java.io.IOException
java.io.IOException
public Table(java.io.InputStream input) throws java.io.IOException
java.io.IOException
public Table(java.io.InputStream input, java.lang.String options) throws java.io.IOException
input
- options
- java.io.IOException
public Table(java.lang.Iterable<TableRow> rows)
public Table(java.sql.ResultSet rs)
public Table typedParse(java.io.InputStream input, java.lang.String options) throws java.io.IOException
java.io.IOException
public static java.lang.String extensionOptions(boolean loading, java.lang.String filename, java.lang.String options)
public void parseInto(java.lang.Object enclosingObject, java.lang.String fieldName)
public boolean save(java.io.File file, java.lang.String options) throws java.io.IOException
java.io.IOException
public boolean save(java.io.OutputStream output, java.lang.String options)
public void addColumn()
removeColumn(String)
public void addColumn(java.lang.String title)
title
- the title to be used for the new columnpublic void addColumn(java.lang.String title, int type)
type
- the type to be used for the new column: INT, LONG, FLOAT, DOUBLE, or STRINGpublic void insertColumn(int index)
public void insertColumn(int index, java.lang.String title)
public void insertColumn(int index, java.lang.String title, int type)
public void removeColumn(java.lang.String columnName)
columnName
- the title of the column to be removedaddColumn()
public void removeColumn(int column)
column
- the index number of the column to be removedpublic int getColumnCount()
getRowCount()
public void setColumnCount(int newCount)
newCount
- public void setColumnType(java.lang.String columnName, java.lang.String columnType)
public void setColumnType(int column, java.lang.String columnType)
column
- the column to changecolumnType
- One of int, long, float, double, string, or category.public void setColumnType(java.lang.String columnName, int newType)
public void setColumnType(int column, int newType)
column
- the column whose type should be changednewType
- something fresh, maybe try an int or a float for size?public void setTableType(java.lang.String type)
public void setColumnTypes(int[] types)
public void setColumnTypes(Table dictionary)
dictionary
- public int getColumnType(java.lang.String columnName)
public int getColumnType(int column)
public int[] getColumnTypes()
@Deprecated public java.lang.String[] removeTitleRow()
public void setColumnTitles(java.lang.String[] titles)
public void setColumnTitle(int column, java.lang.String title)
public boolean hasColumnTitles()
public java.lang.String[] getColumnTitles()
public java.lang.String getColumnTitle(int col)
public int getColumnIndex(java.lang.String columnName)
public int checkColumnIndex(java.lang.String title)
title
- column titlepublic int getRowCount()
getColumnCount()
public int lastRowIndex()
public void clearRows()
addRow()
,
removeRow(int)
public void setRowCount(int newCount)
public TableRow addRow()
removeRow(int)
,
clearRows()
public TableRow addRow(TableRow source)
source
- a reference to the original row to be duplicatedpublic TableRow addRow(java.lang.Object[] columnData)
public void addRows(Table source)
public void insertRow(int insert, java.lang.Object[] columnData)
public void removeRow(int row)
row
- ID number of the row to removeaddRow()
,
clearRows()
public void setRow(int row, java.lang.Object[] pieces)
public TableRow getRow(int row)
row
- ID number of the row to getrows()
,
findRow(String, int)
,
findRows(String, int)
,
matchRow(String, int)
,
matchRows(String, int)
public java.lang.Iterable<TableRow> rows()
getRow(int)
,
findRow(String, int)
,
findRows(String, int)
,
matchRow(String, int)
,
matchRows(String, int)
public java.lang.Iterable<TableRow> rows(int[] indices)
public int getInt(int row, int column)
row
- ID number of the row to referencecolumn
- ID number of the column to referencegetFloat(int, int)
,
getString(int, int)
,
getStringColumn(String)
,
setInt(int, int, int)
,
setFloat(int, int, float)
,
setString(int, int, String)
public int getInt(int row, java.lang.String columnName)
columnName
- title of the column to referencepublic void setMissingInt(int value)
public void setInt(int row, int column, int value)
row
- ID number of the target rowcolumn
- ID number of the target columnvalue
- value to assignsetFloat(int, int, float)
,
setString(int, int, String)
,
getInt(int, int)
,
getFloat(int, int)
,
getString(int, int)
,
getStringColumn(String)
public void setInt(int row, java.lang.String columnName, int value)
columnName
- title of the target columnpublic int[] getIntColumn(java.lang.String name)
public int[] getIntColumn(int col)
public int[] getIntRow(int row)
public long getLong(int row, int column)
public long getLong(int row, java.lang.String columnName)
public void setMissingLong(long value)
public void setLong(int row, int column, long value)
public void setLong(int row, java.lang.String columnName, long value)
public long[] getLongColumn(java.lang.String name)
public long[] getLongColumn(int col)
public long[] getLongRow(int row)
public float getFloat(int row, int column)
row
- ID number of the row to referencecolumn
- ID number of the column to referencegetInt(int, int)
,
getString(int, int)
,
getStringColumn(String)
,
setInt(int, int, int)
,
setFloat(int, int, float)
,
setString(int, int, String)
public float getFloat(int row, java.lang.String columnName)
columnName
- title of the column to referencepublic void setMissingFloat(float value)
public void setFloat(int row, int column, float value)
row
- ID number of the target rowcolumn
- ID number of the target columnvalue
- value to assignsetInt(int, int, int)
,
setString(int, int, String)
,
getInt(int, int)
,
getFloat(int, int)
,
getString(int, int)
,
getStringColumn(String)
public void setFloat(int row, java.lang.String columnName, float value)
columnName
- title of the target columnpublic float[] getFloatColumn(java.lang.String name)
public float[] getFloatColumn(int col)
public float[] getFloatRow(int row)
public double getDouble(int row, int column)
public double getDouble(int row, java.lang.String columnName)
public void setMissingDouble(double value)
public void setDouble(int row, int column, double value)
public void setDouble(int row, java.lang.String columnName, double value)
public double[] getDoubleColumn(java.lang.String name)
public double[] getDoubleColumn(int col)
public double[] getDoubleRow(int row)
public java.lang.String getString(int row, int column)
row
- ID number of the row to referencecolumn
- ID number of the column to referencegetInt(int, int)
,
getFloat(int, int)
,
getStringColumn(String)
,
setInt(int, int, int)
,
setFloat(int, int, float)
,
setString(int, int, String)
public java.lang.String getString(int row, java.lang.String columnName)
columnName
- title of the column to referencepublic void setMissingString(java.lang.String value)
public void setString(int row, int column, java.lang.String value)
row
- ID number of the target rowcolumn
- ID number of the target columnvalue
- value to assignsetInt(int, int, int)
,
setFloat(int, int, float)
,
getInt(int, int)
,
getFloat(int, int)
,
getString(int, int)
,
getStringColumn(String)
public void setString(int row, java.lang.String columnName, java.lang.String value)
columnName
- title of the target columnpublic java.lang.String[] getStringColumn(java.lang.String columnName)
columnName
- title of the column to searchgetInt(int, int)
,
getFloat(int, int)
,
getString(int, int)
,
setInt(int, int, int)
,
setFloat(int, int, float)
,
setString(int, int, String)
public java.lang.String[] getStringColumn(int column)
column
- ID number of the column to searchpublic java.lang.String[] getStringRow(int row)
public int findRowIndex(java.lang.String value, int column)
value
- the String to matchcolumn
- ID number of the column to searchpublic int findRowIndex(java.lang.String value, java.lang.String columnName)
value
- the String to matchcolumnName
- title of the column to searchpublic int[] findRowIndices(java.lang.String value, int column)
value
- the String to matchcolumn
- ID number of the column to searchpublic int[] findRowIndices(java.lang.String value, java.lang.String columnName)
value
- the String to matchcolumnName
- title of the column to searchpublic TableRow findRow(java.lang.String value, int column)
value
- the value to matchcolumn
- ID number of the column to searchgetRow(int)
,
rows()
,
findRows(String, int)
,
matchRow(String, int)
,
matchRows(String, int)
public TableRow findRow(java.lang.String value, java.lang.String columnName)
columnName
- title of the column to searchpublic java.lang.Iterable<TableRow> findRows(java.lang.String value, int column)
value
- the value to matchcolumn
- ID number of the column to searchgetRow(int)
,
rows()
,
findRow(String, int)
,
matchRow(String, int)
,
matchRows(String, int)
public java.lang.Iterable<TableRow> findRows(java.lang.String value, java.lang.String columnName)
columnName
- title of the column to searchpublic java.util.Iterator<TableRow> findRowIterator(java.lang.String value, int column)
value
- the value to matchcolumn
- ID number of the column to searchpublic java.util.Iterator<TableRow> findRowIterator(java.lang.String value, java.lang.String columnName)
columnName
- title of the column to searchpublic int matchRowIndex(java.lang.String regexp, int column)
regexp
- the String to matchcolumn
- ID number of the column to searchpublic int matchRowIndex(java.lang.String what, java.lang.String columnName)
what
- the String to matchcolumnName
- title of the column to searchpublic int[] matchRowIndices(java.lang.String regexp, int column)
regexp
- the String to matchcolumn
- ID number of the column to searchpublic int[] matchRowIndices(java.lang.String what, java.lang.String columnName)
what
- the String to matchcolumnName
- title of the column to searchpublic TableRow matchRow(java.lang.String regexp, int column)
regexp
- the regular expression to matchcolumn
- ID number of the column to searchgetRow(int)
,
rows()
,
findRow(String, int)
,
findRows(String, int)
,
matchRows(String, int)
public TableRow matchRow(java.lang.String regexp, java.lang.String columnName)
columnName
- title of the column to searchpublic java.lang.Iterable<TableRow> matchRows(java.lang.String regexp, int column)
regexp
- the regular expression to matchcolumn
- ID number of the column to searchgetRow(int)
,
rows()
,
findRow(String, int)
,
findRows(String, int)
,
matchRow(String, int)
public java.lang.Iterable<TableRow> matchRows(java.lang.String regexp, java.lang.String columnName)
columnName
- title of the column to searchpublic java.util.Iterator<TableRow> matchRowIterator(java.lang.String value, int column)
value
- the regular expression to matchcolumn
- ID number of the column to searchpublic java.util.Iterator<TableRow> matchRowIterator(java.lang.String value, java.lang.String columnName)
columnName
- title of the column to searchpublic void replace(java.lang.String orig, java.lang.String replacement)
orig
- replacement
- public void replace(java.lang.String orig, java.lang.String replacement, int col)
public void replace(java.lang.String orig, java.lang.String replacement, java.lang.String colName)
public void replaceAll(java.lang.String regex, java.lang.String replacement)
public void replaceAll(java.lang.String regex, java.lang.String replacement, int column)
public void replaceAll(java.lang.String regex, java.lang.String replacement, java.lang.String columnName)
regex
- the String to matchcolumnName
- title of the column to searchpublic void removeTokens(java.lang.String tokens)
tokens
- a list of individual characters to be removedtrim()
public void removeTokens(java.lang.String tokens, int column)
table.removeTokens(",$", 2);
column
- ID number of the column to processpublic void removeTokens(java.lang.String tokens, java.lang.String columnName)
columnName
- title of the column to processpublic void trim()
removeTokens(String)
public void trim(int column)
column
- ID number of the column to trimpublic void trim(java.lang.String columnName)
columnName
- title of the column to trimpublic void sort(java.lang.String columnName)
columnName
- the name of the column to sorttrim()
public void sort(int column)
column
- the column ID, e.g. 0, 1, 2public void sortReverse(java.lang.String columnName)
public void sortReverse(int column)
public java.lang.String[] getUnique(java.lang.String columnName)
public java.lang.String[] getUnique(int column)
public IntDict getTally(java.lang.String columnName)
public IntDict getTally(int column)
public IntDict getOrder(java.lang.String columnName)
public IntDict getOrder(int column)
public IntList getIntList(java.lang.String columnName)
public IntList getIntList(int column)
public FloatList getFloatList(java.lang.String columnName)
public FloatList getFloatList(int column)
public StringList getStringList(java.lang.String columnName)
public StringList getStringList(int column)
public IntDict getIntDict(java.lang.String keyColumnName, java.lang.String valueColumnName)
public IntDict getIntDict(int keyColumn, int valueColumn)
public FloatDict getFloatDict(java.lang.String keyColumnName, java.lang.String valueColumnName)
public FloatDict getFloatDict(int keyColumn, int valueColumn)
public StringDict getStringDict(java.lang.String keyColumnName, java.lang.String valueColumnName)
public StringDict getStringDict(int keyColumn, int valueColumn)
public java.util.Map<java.lang.String,TableRow> getRowMap(java.lang.String columnName)
public java.util.Map<java.lang.String,TableRow> getRowMap(int column)
Table t = loadTable("country-data.tsv", "header"); // use the contents of the 'country' column to index the table Maplookup = t.getRowMap("country"); // get the row that has "us" in the "country" column: TableRow usRow = lookup.get("us"); // get an entry from the 'population' column int population = usRow.getInt("population");
public Table copy()
public void write(java.io.PrintWriter writer)
public void print()