public class Table
extends 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(File file) |
Table(File file,
String options)
version that uses a File object; future releases (or data types)
may include additional optimizations here
|
Table(InputStream input) |
Table(InputStream input,
String options)
Read the table from a stream.
|
Table(Iterable<TableRow> rows) |
Table(ResultSet rs) |
Modifier and Type | Method and Description |
---|---|
void |
addColumn() |
void |
addColumn(String title) |
void |
addColumn(String title,
int type) |
TableRow |
addRow() |
TableRow |
addRow(Object[] columnData) |
TableRow |
addRow(TableRow source) |
void |
addRows(Table source) |
int |
checkColumnIndex(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 String |
extensionOptions(boolean loading,
String filename,
String options) |
TableRow |
findRow(String value,
int column) |
TableRow |
findRow(String value,
String columnName) |
int |
findRowIndex(String value,
int column)
Return the row that contains the first String that matches.
|
int |
findRowIndex(String value,
String columnName)
Return the row that contains the first String that matches.
|
int[] |
findRowIndices(String value,
int column)
Return a list of rows that contain the String passed in.
|
int[] |
findRowIndices(String value,
String columnName)
Return a list of rows that contain the String passed in.
|
Iterator<TableRow> |
findRowIterator(String value,
int column) |
Iterator<TableRow> |
findRowIterator(String value,
String columnName) |
Iterable<TableRow> |
findRows(String value,
int column) |
Iterable<TableRow> |
findRows(String value,
String columnName) |
int |
getColumnCount() |
int |
getColumnIndex(String columnName) |
String |
getColumnTitle(int col) |
String[] |
getColumnTitles() |
int |
getColumnType(int column)
Returns one of Table.STRING, Table.INT, etc...
|
int |
getColumnType(String columnName) |
int[] |
getColumnTypes() |
double |
getDouble(int row,
int column) |
double |
getDouble(int row,
String columnName) |
double[] |
getDoubleColumn(int col) |
double[] |
getDoubleColumn(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,
String columnName) |
float[] |
getFloatColumn(int col) |
float[] |
getFloatColumn(String name) |
FloatDict |
getFloatDict(int keyColumn,
int valueColumn) |
FloatDict |
getFloatDict(String keyColumnName,
String valueColumnName) |
FloatList |
getFloatList(int column) |
FloatList |
getFloatList(String columnName) |
float[] |
getFloatRow(int row) |
int |
getInt(int row,
int column) |
int |
getInt(int row,
String columnName) |
int[] |
getIntColumn(int col) |
int[] |
getIntColumn(String name) |
IntDict |
getIntDict(int keyColumn,
int valueColumn) |
IntDict |
getIntDict(String keyColumnName,
String valueColumnName) |
IntList |
getIntList(int column) |
IntList |
getIntList(String columnName) |
int[] |
getIntRow(int row) |
long |
getLong(int row,
int column) |
long |
getLong(int row,
String columnName) |
long[] |
getLongColumn(int col) |
long[] |
getLongColumn(String name) |
long[] |
getLongRow(int row) |
IntDict |
getOrder(int column) |
IntDict |
getOrder(String columnName) |
TableRow |
getRow(int row) |
int |
getRowCount() |
Map<String,TableRow> |
getRowMap(int column)
Return a mapping that connects the entry from a column back to the row
from which it came.
|
Map<String,TableRow> |
getRowMap(String columnName) |
String |
getString(int row,
int column)
Get a String value from the table.
|
String |
getString(int row,
String columnName) |
String[] |
getStringColumn(int column) |
String[] |
getStringColumn(String columnName) |
StringDict |
getStringDict(int keyColumn,
int valueColumn) |
StringDict |
getStringDict(String keyColumnName,
String valueColumnName) |
StringList |
getStringList(int column) |
StringList |
getStringList(String columnName) |
String[] |
getStringRow(int row) |
IntDict |
getTally(int column) |
IntDict |
getTally(String columnName) |
String[] |
getUnique(int column) |
String[] |
getUnique(String columnName) |
boolean |
hasColumnTitles() |
void |
insertColumn(int index) |
void |
insertColumn(int index,
String title) |
void |
insertColumn(int index,
String title,
int type) |
void |
insertRow(int insert,
Object[] columnData) |
int |
lastRowIndex() |
TableRow |
matchRow(String regexp,
int column) |
TableRow |
matchRow(String regexp,
String columnName) |
int |
matchRowIndex(String regexp,
int column)
Return the row that contains the first String that matches.
|
int |
matchRowIndex(String what,
String columnName)
Return the row that contains the first String that matches.
|
int[] |
matchRowIndices(String regexp,
int column)
Return a list of rows that contain the String passed in.
|
int[] |
matchRowIndices(String what,
String columnName)
Return a list of rows that match the regex passed in.
|
Iterator<TableRow> |
matchRowIterator(String value,
int column) |
Iterator<TableRow> |
matchRowIterator(String value,
String columnName) |
Iterable<TableRow> |
matchRows(String regexp,
int column) |
Iterable<TableRow> |
matchRows(String regexp,
String columnName) |
void |
parseInto(Object enclosingObject,
String fieldName)
incomplete, do not use
|
void |
print() |
void |
removeColumn(int column) |
void |
removeColumn(String columnName) |
void |
removeRow(int row) |
String[] |
removeTitleRow()
Deprecated.
|
void |
removeTokens(String tokens)
Remove any of the specified characters from the entire table.
|
void |
removeTokens(String tokens,
int column)
Removed any of the specified characters from a column.
|
void |
removeTokens(String tokens,
String columnName) |
void |
replace(String orig,
String replacement)
Replace a String with another.
|
void |
replace(String orig,
String replacement,
int col) |
void |
replace(String orig,
String replacement,
String colName) |
void |
replaceAll(String regex,
String replacement) |
void |
replaceAll(String regex,
String replacement,
int column) |
void |
replaceAll(String regex,
String replacement,
String columnName)
Run String.replaceAll() on all entries in a column.
|
Iterable<TableRow> |
rows()
Note that this one iterator instance is shared by any calls to iterate
the rows of this table.
|
Iterable<TableRow> |
rows(int[] indices) |
boolean |
save(File file,
String options) |
boolean |
save(OutputStream output,
String options) |
void |
setColumnCount(int newCount)
Change the number of columns in this table.
|
void |
setColumnTitle(int column,
String title) |
void |
setColumnTitles(String[] titles) |
void |
setColumnType(int column,
int newType)
Sets the column type.
|
void |
setColumnType(int column,
String columnType)
Set the data type for a column so that using it is more efficient.
|
void |
setColumnType(String columnName,
int newType) |
void |
setColumnType(String columnName,
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,
String columnName,
double value) |
void |
setFloat(int row,
int column,
float value) |
void |
setFloat(int row,
String columnName,
float value) |
void |
setInt(int row,
int column,
int value) |
void |
setInt(int row,
String columnName,
int value) |
void |
setLong(int row,
int column,
long value) |
void |
setLong(int row,
String columnName,
long value) |
void |
setMissingDouble(double value) |
void |
setMissingFloat(float value) |
void |
setMissingInt(int value) |
void |
setMissingLong(long value) |
void |
setMissingString(String value)
Treat entries with this string as "missing".
|
void |
setRow(int row,
Object[] pieces) |
TableRow |
setRow(int row,
TableRow source) |
void |
setRowCount(int newCount) |
void |
setString(int row,
int column,
String value) |
void |
setString(int row,
String columnName,
String value) |
void |
setTableType(String type)
Set the entire table to a specific data type.
|
void |
sort(int column) |
void |
sort(String columnName)
Sorts (orders) a table based on the values in a column.
|
void |
sortReverse(int column) |
void |
sortReverse(String columnName) |
void |
trim() |
void |
trim(int column) |
void |
trim(String columnName) |
Table |
typedParse(InputStream input,
String options) |
void |
write(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(File file) throws IOException
IOException
public Table(File file, String options) throws IOException
IOException
public Table(InputStream input) throws IOException
IOException
public Table(InputStream input, String options) throws IOException
input
- options
- IOException
public Table(Iterable<TableRow> rows)
public Table(ResultSet rs)
public Table typedParse(InputStream input, String options) throws IOException
IOException
public static String extensionOptions(boolean loading, String filename, String options)
public void parseInto(Object enclosingObject, String fieldName)
public boolean save(File file, String options) throws IOException
IOException
public boolean save(OutputStream output, String options)
public void addColumn()
removeColumn(String)
public void addColumn(String title)
title
- the title to be used for the new columnpublic void addColumn(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, String title)
public void insertColumn(int index, String title, int type)
public void removeColumn(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(String columnName, String columnType)
public void setColumnType(int column, String columnType)
column
- the column to changecolumnType
- One of int, long, float, double, string, or category.public void setColumnType(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(String type)
public void setColumnTypes(int[] types)
public void setColumnTypes(Table dictionary)
dictionary
- public int getColumnType(String columnName)
public int getColumnType(int column)
public int[] getColumnTypes()
@Deprecated public String[] removeTitleRow()
public void setColumnTitles(String[] titles)
public void setColumnTitle(int column, String title)
public boolean hasColumnTitles()
public String[] getColumnTitles()
public String getColumnTitle(int col)
public int getColumnIndex(String columnName)
public int checkColumnIndex(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(Object[] columnData)
public void addRows(Table source)
public void insertRow(int insert, Object[] columnData)
public void removeRow(int row)
row
- ID number of the row to removeaddRow()
,
clearRows()
public void setRow(int row, 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 Iterable<TableRow> rows()
getRow(int)
,
findRow(String, int)
,
findRows(String, int)
,
matchRow(String, int)
,
matchRows(String, int)
public 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, 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, String columnName, int value)
columnName
- title of the target columnpublic int[] getIntColumn(String name)
public int[] getIntColumn(int col)
public int[] getIntRow(int row)
public long getLong(int row, int column)
public long getLong(int row, String columnName)
public void setMissingLong(long value)
public void setLong(int row, int column, long value)
public void setLong(int row, String columnName, long value)
public long[] getLongColumn(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, 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, String columnName, float value)
columnName
- title of the target columnpublic float[] getFloatColumn(String name)
public float[] getFloatColumn(int col)
public float[] getFloatRow(int row)
public double getDouble(int row, int column)
public double getDouble(int row, String columnName)
public void setMissingDouble(double value)
public void setDouble(int row, int column, double value)
public void setDouble(int row, String columnName, double value)
public double[] getDoubleColumn(String name)
public double[] getDoubleColumn(int col)
public double[] getDoubleRow(int row)
public 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 String getString(int row, String columnName)
columnName
- title of the column to referencepublic void setMissingString(String value)
public void setString(int row, int column, 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, String columnName, String value)
columnName
- title of the target columnpublic String[] getStringColumn(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 String[] getStringColumn(int column)
column
- ID number of the column to searchpublic String[] getStringRow(int row)
public int findRowIndex(String value, int column)
value
- the String to matchcolumn
- ID number of the column to searchpublic int findRowIndex(String value, String columnName)
value
- the String to matchcolumnName
- title of the column to searchpublic int[] findRowIndices(String value, int column)
value
- the String to matchcolumn
- ID number of the column to searchpublic int[] findRowIndices(String value, String columnName)
value
- the String to matchcolumnName
- title of the column to searchpublic TableRow findRow(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(String value, String columnName)
columnName
- title of the column to searchpublic Iterable<TableRow> findRows(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 Iterable<TableRow> findRows(String value, String columnName)
columnName
- title of the column to searchpublic Iterator<TableRow> findRowIterator(String value, int column)
value
- the value to matchcolumn
- ID number of the column to searchpublic Iterator<TableRow> findRowIterator(String value, String columnName)
columnName
- title of the column to searchpublic int matchRowIndex(String regexp, int column)
regexp
- the String to matchcolumn
- ID number of the column to searchpublic int matchRowIndex(String what, String columnName)
what
- the String to matchcolumnName
- title of the column to searchpublic int[] matchRowIndices(String regexp, int column)
regexp
- the String to matchcolumn
- ID number of the column to searchpublic int[] matchRowIndices(String what, String columnName)
what
- the String to matchcolumnName
- title of the column to searchpublic TableRow matchRow(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(String regexp, String columnName)
columnName
- title of the column to searchpublic Iterable<TableRow> matchRows(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 Iterable<TableRow> matchRows(String regexp, String columnName)
columnName
- title of the column to searchpublic Iterator<TableRow> matchRowIterator(String value, int column)
value
- the regular expression to matchcolumn
- ID number of the column to searchpublic Iterator<TableRow> matchRowIterator(String value, String columnName)
columnName
- title of the column to searchpublic void replace(String orig, String replacement)
orig
- replacement
- public void replace(String orig, String replacement, int col)
public void replace(String orig, String replacement, String colName)
public void replaceAll(String regex, String replacement)
public void replaceAll(String regex, String replacement, int column)
public void replaceAll(String regex, String replacement, String columnName)
regex
- the String to matchcolumnName
- title of the column to searchpublic void removeTokens(String tokens)
tokens
- a list of individual characters to be removedtrim()
public void removeTokens(String tokens, int column)
table.removeTokens(",$", 2);
column
- ID number of the column to processpublic void removeTokens(String tokens, 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(String columnName)
columnName
- title of the column to trimpublic void sort(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(String columnName)
public void sortReverse(int column)
public String[] getUnique(String columnName)
public String[] getUnique(int column)
public IntDict getTally(String columnName)
public IntDict getTally(int column)
public IntDict getOrder(String columnName)
public IntDict getOrder(int column)
public IntList getIntList(String columnName)
public IntList getIntList(int column)
public FloatList getFloatList(String columnName)
public FloatList getFloatList(int column)
public StringList getStringList(String columnName)
public StringList getStringList(int column)
public IntDict getIntDict(String keyColumnName, String valueColumnName)
public IntDict getIntDict(int keyColumn, int valueColumn)
public FloatDict getFloatDict(String keyColumnName, String valueColumnName)
public FloatDict getFloatDict(int keyColumn, int valueColumn)
public StringDict getStringDict(String keyColumnName, String valueColumnName)
public StringDict getStringDict(int keyColumn, int valueColumn)
public Map<String,TableRow> getRowMap(String columnName)
public Map<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(PrintWriter writer)
public void print()