public class Data
class |
Data.Row
Contains one row of values. |
void |
addCol (String hdr)
Modify column headers to add new column. |
void |
addCol (String hdr, Formula<?> calc)
Modify column headers to add new column. |
void |
addCol (String hdr, String value)
Modify column headers to add new column. |
Data.Row |
addRow ()
Create new empty data row. |
Data.Row |
addRow (String... vs)
Create new data row and initialise with values. |
void |
append (Data other)
Append data from another table respecting column names. |
void |
appendMarked (Data other, String markHdr, String mark)
Append and mark data from another table respecting column names. |
void |
clear ()
Remove all data rows. |
int |
colCount ()
Get number of columns. |
Data |
copy ()
|
Data |
copyCols (String[] headers)
|
int |
count ()
Get count of data rows. |
Data |
filter (Filter filter)
|
int |
findCol (String hdr)
Find column index by column name. |
Data.Row |
firstRow ()
Get the first data row or null if the table is empty. |
<T>void |
fold (Formula.Var<T> acc, Formula<T> calc)
|
String |
getHeader (int index)
Get column name by column index. |
Data |
groupBy (String hdr, String[] headers, Fold<?>... folds)
|
int[] |
indexMap (String[] hdrs)
Find column indices by column names. |
Data.Row |
lastRow ()
Get the last data row or null if the table is empty. |
Data |
limit (int count)
|
int[] |
optionalIndexMap (String[] hdrs)
Find column indices by column names without throwing an exception. |
void |
print ()
|
void |
print (PrintStream out, boolean withHeaders)
|
void |
printCols (PrintStream out, boolean withHeaders, String... cols)
|
void |
printCols (String... cols)
|
void |
printHeaders (PrintStream out)
|
void |
printHeaders (PrintStream out, String[] cols)
|
void |
recalc (String hdr, Formula<?> calc)
Recalculate contents of a column using formula. |
void |
renameHeader (String oldName, String newName)
Rename column. |
void |
renameHeaders (String[] headers)
Rename all headers. |
void |
replaceAll (String hdr, String oldValue, String newValue)
Find and replace values in a column. |
Iterable<Data.Row> |
rows ()
Get data rows as unmodifiable iterable. |
void |
sort (boolean asc, Formula<?>... calc)
|
void |
sort (boolean asc, String... hdr)
|
void |
sort (Formula<?>... calc)
|
void |
sort (String... hdr)
|
Show all inherited methods (11 more) |
static Data |
range (String hdr, int min, int max)
|
static Data |
range (String hdr, int min, int max, int step)
|
static Data |
range (String hdr, String fmt, double min, double max, double step)
|
static Data |
read (File file)
|
static Data |
read (File file, String[] headers)
|
static Data |
read (File file, String[] headers, String sepRegex)
|
static void |
write (Data data, File file)
|
public Data ( String... headers );
Class constructor.
headers
InvalidParameterException
public void renameHeaders ( String[] headers );
Rename all headers.
headers
InvalidParameterException
headers
does not match the number of columnsInvalidParameterException
public void renameHeader ( String oldName, String newName );
Rename column.
oldName
newName
NoSuchElementException
oldName
InvalidParameterException
newName
already existspublic String getHeader ( int index );
Get column name by column index.
index
column name
ArrayIndexOutOfBoundsException
index<0
or index>=colCount()
public Data.Row addRow ( String... vs );
Create new data row and initialise with values.
Copying is range-safe in case the number of values in vs
does not match the number of columns.
vs
new row
public Data.Row firstRow ();
Get the first data row or null
if the table is empty.
data row
public Data.Row lastRow ();
Get the last data row or null
if the table is empty.
data row
public int findCol ( String hdr );
Find column index by column name. Lookup is relatively fast as it uses a hash map.
hdr
column index
NoSuchElementException
public int[] indexMap ( String[] hdrs );
Find column indices by column names. Throws an exception if any of the names is not found.
hdrs
null
array of column indices in the order of hdrs
NoSuchElementException
NullPointerException
hdrs
is null
public int[] optionalIndexMap ( String[] hdrs );
Find column indices by column names without throwing an exception. If a column name does not exist, -1
index is returned.
hdrs
null
array of column indices in the order of hdrs
NullPointerException
hdrs
is null
public void addCol ( String hdr, Formula<?> calc );
Modify column headers to add new column.
Values of the new column can be calculated using calc
or initalised to null
if formula is not provided.
hdr
calc
null
public void addCol ( String hdr, String value );
Modify column headers to add new column. Values of the new column are initialised to the given constant.
hdr
value
null
public void addCol ( String hdr );
Modify column headers to add new column. Values of the new column are initialised to null
.
hdr
public void recalc ( String hdr, Formula<?> calc );
Recalculate contents of a column using formula. If formula is not provided, column values are set to null
.
hdr
calc
null
public void replaceAll ( String hdr, String oldValue, String newValue );
Find and replace values in a column. Uses case-sensitive string comparison for non-null values.
hdr
oldValue
null
newValue
null
public void append ( Data other );
Append data from another table respecting column names.
other
NullPointerException
null
public void appendMarked ( Data other, String markHdr, String mark );
Append and mark data from another table respecting column names. Appended rows are marked with a specified value.
Typical use for this method is to keep track of data sources when combining data from multiple tables. For example:
Data data = Data.read(new File("data1.csv")); data.addCol("source", "data1"); data.appendMarked(Data.read(new File("data2.csv")), "source", "data2"); data.appendMarked(Data.read(new File("data3.csv")), "source", "data3");
other
markHdr
mark
null
NullPointerException
null
NoSuchElementException
markHdr
does not exist
Data table.
Values are stored as nullable strings in rows and columns. Columns can be accessed by name (header).