public class CSVDataTable extends Object implements DataTable
DataTable.HashedRow, DataTable.Row
Constructor and Description |
---|
CSVDataTable()
This constructor creates an empty DataTable with no columns.
|
CSVDataTable(File file)
Create a new data table using CSV data from a file.
|
CSVDataTable(File file,
boolean firstRowNames)
Create a new data table using CSV data from a file.
|
CSVDataTable(File file,
boolean firstRowNames,
char delimiter)
Create a new data table using CSV data on a website.
|
CSVDataTable(InputStream in,
boolean firstRowNames,
char delimiter)
Create a new data table using CSV data from an input stream.
|
CSVDataTable(String url)
Create a new data table using CSV data on a website.
|
CSVDataTable(String url,
boolean firstRowNames)
Create a new data table using CSV data on a website.
|
CSVDataTable(String url,
boolean firstRowNames,
char delimiter)
Create a new data table using CSV data on a website.
|
Modifier and Type | Method and Description |
---|---|
void |
addColumn(String columnName)
This method adds a column to the table.
|
void |
addColumn(String columnName,
String defaultValue)
This method adds a column to the table, setting this column to the
specified default value in every row.
|
void |
addRow(DataTable.Row row)
This method adds a row to the table.
|
double |
avg(String column)
This method computes the average of a column that has numeric
values.
|
int |
colCount()
This method returns the number of columns in the data table.
|
void |
copyRowsFrom(DataTable dataSet)
This method copies an existing DataTable's rows into the
DataTable.
|
int |
count(String column,
String value)
This method counts the number of rows in the specified column
that have the value given by the value parameter.
|
List<DataTable.Row> |
getAllRows()
This method returns a representation of the Data Table.
|
String |
getCell(int row,
int column)
This method returns the String value in the table at a specified
row/column location.
|
String |
getCell(int row,
String column)
This method returns the String value in the table at a specified
row/column location.
|
String |
getCell(String row,
int column)
This method returns the String value in the table at a specified
row/column location.
|
String |
getCell(String row,
String column)
This method returns the String value in the table at a specified
row/column location.
|
List<String> |
getColumnNames()
This method returns a list of the names of all of the
columns of the data table.
|
char |
getDelimiter()
Get the delimiter used by this data set.
|
double |
getDoubleCell(int row,
int column)
This method returns the double value in the table at a specified
row/column location.
|
double |
getDoubleCell(int row,
String column)
This method returns the double value in the table at a specified
row/column location.
|
double |
getDoubleCell(String row,
int column)
This method returns the double value in the table at a specified
row/column location.
|
double |
getDoubleCell(String row,
String column)
This method returns the double value in the table at a specified
row/column location.
|
int |
getIntCell(int row,
int column)
This method returns the int value in the table at a specified
row/column location.
|
int |
getIntCell(int row,
String column)
This method returns the int value in the table at a specified
row/column location.
|
int |
getIntCell(String row,
int column)
This method returns the int value in the table at a specified
row/column location.
|
int |
getIntCell(String row,
String column)
This method returns the int value in the table at a specified
row/column location.
|
String |
getPrimaryKey()
This method returns the column name that acts as the primary key for
the table.
|
DataTable.Row |
getRow(int row)
This method returns a map that represents the desired
row (indicated by the primary key).
|
DataTable.Row |
getRow(String row)
This method returns a Map that represents the desired
row.
|
String |
getUrl()
Get the URL used to load this data table.
|
Iterator<DataTable.Row> |
iterator() |
double |
max(String column)
This method computes the maximum value of a column that has
numeric values.
|
double |
min(String column)
This method computes the minimum value of a column that has
numeric values.
|
void |
remapColumnNames(Map<String,String> columnMap)
This method replaces each column name in the table with a
matching key in columnMap with the value of the mapping.
|
DataTable.Row |
removeRow(int row)
This method removes the row identified by row number.
|
DataTable.Row |
removeRow(String row)
This method removes the row (to be determined by the key).
|
int |
rowCount()
This method returns the number of rows in the data table.
|
void |
save(String filename)
This method saves the CSVDataTable in a CSV file format
with the first row the column names and the remaining rows
the data in the table.
|
void |
setCell(int row,
int column,
double value)
This method allows you to update a desired row and column with
new data as a double.
|
void |
setCell(int row,
int column,
int value)
This method allows you to update a desired row and column with
new data as an int.
|
void |
setCell(int row,
int column,
String value)
This method allows you to update a desired row and column with
new data as a String.
|
void |
setCell(int row,
String column,
double value)
This method allows you to update a desired row and column with
new data as a double.
|
void |
setCell(int row,
String column,
int value)
This method allows you to update a desired row and column with
new data as an int.
|
void |
setCell(int row,
String column,
String value)
This method allows you to update a desired row and column with
new data as a String.
|
void |
setCell(String row,
int column,
double value)
This method allows you to update a desired row and column with
new data as a double.
|
void |
setCell(String row,
int column,
int value)
This method allows you to update a desired row and column with
new data as an int.
|
void |
setCell(String row,
int column,
String value)
This method allows you to update a desired row and column with
new data as a String.
|
void |
setCell(String row,
String column,
double value)
This method allows you to update a desired row and column with
new data as a double.
|
void |
setCell(String row,
String column,
int value)
This method allows you to update a desired row and column with
new data as an int.
|
void |
setCell(String row,
String column,
String value)
This method allows you to update a desired row and column with
new data as a String.
|
void |
setColumnNames(List<String> columns)
This method sets an arbitrary name for each of the columns
in the table.
|
void |
setColumnNames(String... columns)
This method sets an arbitrary name for each of the columns
in the table.
|
void |
setDelimiter(char delimiter)
Set the delimiter used by this data set, which will affect how
this object will be written to a file in the future.
|
void |
setPrimaryKey(String key)
This method sets the column name that acts as the primary key for the
table.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
public CSVDataTable()
public CSVDataTable(String url)
url
- the URL path to the CSV file.public CSVDataTable(String url, boolean firstRowNames)
url
- the URL path to the CSV file.firstRowNames
- If true, then the first row in the data source
should be treated as containing the names for the columns
in the data set.public CSVDataTable(String url, boolean firstRowNames, char delimiter)
url
- the URL path to the CSV file.firstRowNames
- If true, then the first row in the data source
should be treated as containing the names for the columns
in the data set.delimiter
- The separator between fields on the same line
(e.g., a comma, tab, colon, etc.).public CSVDataTable(File file)
file
- the CSV file to read.public CSVDataTable(File file, boolean firstRowNames)
file
- the CSV file to read.firstRowNames
- If true, then the first row in the data source
should be treated as containing the names for the columns
in the data set.public CSVDataTable(File file, boolean firstRowNames, char delimiter)
file
- the CSV file to read.firstRowNames
- If true, then the first row in the data source
should be treated as containing the names for the columns
in the data set.delimiter
- The separator between fields on the same line
(e.g., a comma, tab, colon, etc.).public CSVDataTable(InputStream in, boolean firstRowNames, char delimiter)
in
- the input stream that contains the CSV data.firstRowNames
- If true, then the first row in the data source
should be treated as containing the names for the columns
in the data set.delimiter
- The separator between fields on the same line
(e.g., a comma, tab, colon, etc.).public int rowCount()
DataTable
public int colCount()
DataTable
public String getCell(int row, String column)
DataTable
public String getCell(int row, int column)
DataTable
public String getCell(String row, String column)
DataTable
public String getCell(String row, int column)
DataTable
public int getIntCell(int row, String column) throws NumberFormatException
DataTable
getIntCell
in interface DataTable
row
- index of row to retrieve (starting at zero)column
- the name of the desired columnNumberFormatException
- if the cell does not contain an int
value.public int getIntCell(int row, int column) throws NumberFormatException
DataTable
getIntCell
in interface DataTable
row
- index of row to retrieve (starting at zero)column
- index of column to retrieve (starting at zero)NumberFormatException
- if the cell does not contain an int
value.public int getIntCell(String row, String column) throws NumberFormatException
DataTable
getIntCell
in interface DataTable
row
- primary key of rowcolumn
- the name of the desired columnNumberFormatException
- if the cell does not contain an int
value.public int getIntCell(String row, int column) throws NumberFormatException
DataTable
getIntCell
in interface DataTable
row
- primary key of rowcolumn
- index of column to retrieve (starting at zero)NumberFormatException
- if the cell does not contain an int
value.public double getDoubleCell(int row, String column) throws NumberFormatException
DataTable
getDoubleCell
in interface DataTable
row
- index of row to retrieve (starting at zero)column
- the name of the desired columnNumberFormatException
- if the cell does not contain a double
value.public double getDoubleCell(int row, int column) throws NumberFormatException
DataTable
getDoubleCell
in interface DataTable
row
- index of row to retrieve (starting at zero)column
- index of column to retrieve (starting at zero)NumberFormatException
- if the cell does not contain a double
value.public double getDoubleCell(String row, String column) throws NumberFormatException
DataTable
getDoubleCell
in interface DataTable
row
- primary key of rowcolumn
- the name of the desired columnNumberFormatException
- if the cell does not contain a double
value.public double getDoubleCell(String row, int column) throws NumberFormatException
DataTable
getDoubleCell
in interface DataTable
row
- primary key of rowcolumn
- index of column to retrieve (starting at zero)NumberFormatException
- if the cell does not contain a double
value.public DataTable.Row getRow(int row)
DataTable
public DataTable.Row getRow(String row)
DataTable
public void setCell(int row, String column, String value)
DataTable
public void setCell(int row, String column, int value)
DataTable
public void setCell(int row, String column, double value)
DataTable
public void setCell(int row, int column, String value)
DataTable
public void setCell(int row, int column, int value)
DataTable
public void setCell(int row, int column, double value)
DataTable
public void setCell(String row, String column, String value)
DataTable
public void setCell(String row, String column, int value)
DataTable
public void setCell(String row, String column, double value)
DataTable
public void setCell(String row, int column, String value)
DataTable
public void setCell(String row, int column, int value)
DataTable
public void setCell(String row, int column, double value)
DataTable
public DataTable.Row removeRow(int row)
DataTable
public DataTable.Row removeRow(String row)
DataTable
public void addColumn(String columnName)
DataTable
public void addColumn(String columnName, String defaultValue)
DataTable
public void addRow(DataTable.Row row)
DataTable
public void setPrimaryKey(String key)
DataTable
setPrimaryKey
in interface DataTable
key
- the column name to be set as the primary key.public String getPrimaryKey()
DataTable
getPrimaryKey
in interface DataTable
public List<String> getColumnNames()
DataTable
getColumnNames
in interface DataTable
public void setColumnNames(List<String> columns)
DataTable
setColumnNames
in interface DataTable
columns
- names of columns to be set in the table.public void setColumnNames(String... columns)
DataTable
setColumnNames
in interface DataTable
columns
- names of columns to be set in the table.public void remapColumnNames(Map<String,String> columnMap)
DataTable
remapColumnNames
in interface DataTable
columnMap
- names of columns to be replaced with the
associated value to replace it with.public List<DataTable.Row> getAllRows()
DataTable
getAllRows
in interface DataTable
public Iterator<DataTable.Row> iterator()
iterator
in interface Iterable<DataTable.Row>
public String getUrl()
public void copyRowsFrom(DataTable dataSet)
DataTable
copyRowsFrom
in interface DataTable
dataSet
- a DataTable object to copy into the current DataTable.public int count(String column, String value)
DataTable
public double avg(String column) throws NumberFormatException
DataTable
avg
in interface DataTable
column
- the column to compute the average of.NumberFormatException
- if the column has a string that cannot
be parsed as a number.public double max(String column) throws NumberFormatException
DataTable
max
in interface DataTable
column
- the column to compute the max number of.NumberFormatException
- if the column has a string that cannot
be parsed as a number.public double min(String column) throws NumberFormatException
DataTable
min
in interface DataTable
column
- the column to compute the min number of.NumberFormatException
- if the column has a string that cannot
be parsed as a number.public void save(String filename)
public void setDelimiter(char delimiter)
delimiter
- The separator between fields on the same line
(e.g., a comma, tab, colon, etc.).public char getDelimiter()