T - the type of values contained in the column this table points topublic class Table<T>
extends java.lang.Object
implements java.lang.Iterable<T>
Each table instance maintains a column name, called the column pointer, and an underlying data structure.
The underlying data is shared between table instances derived from one another.
As such, operations on one table usually cause changes in derived or original tables.
This can be prevented with the copy() operation, which does a full copy of all data such that the copy
is no longer connected to the original.
Various subclasses exist for type-specific operations, e.g. primitive numbers (intTable, longTable,
floatTable, doubleTable), other standard data types (StringTable, BooleanTable),
and model objects (ObjectTable).
| Constructor and Description |
|---|
Table()
Creates a new empty table with zero rows and zero columns.
|
Table(java.lang.String columnName)
Creates a new empty table with zero rows and one column with the given name.
|
Table(java.lang.String columnName,
T... start)
Creates a new table with the given objects in the first column.
|
Table(T... start)
Creates a new table with the given objects in the first column, which uses the default name "A".
|
| Modifier and Type | Method and Description |
|---|---|
<TAB extends Table<T>> |
as(java.lang.Class<? extends TAB> type)
Coerces this table to the given type by creating a new instance of that type that points to the same underlying
data structure and column.
|
Table<T> |
copy()
Creates a copy of this table.
|
<U> Table<U> |
derive(java.lang.String columnName,
java.util.function.Function<? super java.util.Map<java.lang.String,java.lang.Object>,? extends U> function)
Creates a new column with the given name by applying the given function to each row.
|
<U> Table<U> |
deriveAll(java.lang.String columnName,
java.util.function.Function<? super java.util.Map<java.lang.String,java.lang.Object>,? extends java.util.Collection<? extends U>> function)
Creates a new column with the given name by applying the given function to each row, and flattening the result.
|
Table<T> |
dropColumns(java.lang.String... columnNames)
Removes all given columns from the underlying data structure and ensures no duplicate rows exist afterwards.
|
<U> Table<U> |
expand(java.lang.String columnName,
java.util.function.Function<? super T,? extends U> function)
Creates a new column with the given name by applying the given function to each object in the column pointed to by
this table.
|
<U> Table<U> |
expandAll(java.lang.String columnName,
java.util.function.Function<? super T,? extends java.util.Collection<? extends U>> function)
Creates a new column with the given name by applying the given function to each object in the column pointed to by
this table, and flattening the result.
|
Table<T> |
filter(java.util.function.Predicate<? super T> predicate)
Removes all rows from this table for which the predicate returned
false when passed the cell value of the
column this table points to. |
Table<T> |
filterRows(java.util.function.Predicate<? super java.util.Map<java.lang.String,java.lang.Object>> predicate)
Removes all rows from this table for which the predicate returned
false. |
java.util.LinkedHashMap<java.lang.String,java.lang.Integer> |
getColumnMap()
Deprecated.
since 1.2; for internal use only
|
java.lang.String |
getColumnName() |
java.util.ArrayList<java.util.ArrayList<java.lang.Object>> |
getTable()
Deprecated.
since 1.2; for internal use only
|
java.util.Iterator<T> |
iterator() |
int |
rowCount() |
Table<T> |
selectColumns(java.lang.String... columnNames)
Removes all columns from the underlying data structure except the given ones, and ensures no duplicate rows exist
afterwards.
|
java.util.stream.Stream<T> |
stream() |
java.util.List<T> |
toList() |
java.util.Set<T> |
toSet() |
java.lang.String |
toString() |
public Table()
public Table(java.lang.String columnName)
columnName - the column name@SafeVarargs public Table(T... start)
start - the objects in the first column@SafeVarargs
public Table(java.lang.String columnName,
T... start)
columnName - the name of the first columnstart - the objects in the first columnpublic Table<T> copy()
public java.lang.String getColumnName()
public <TAB extends Table<T>> TAB as(java.lang.Class<? extends TAB> type)
The type must be either one of the Table subclasses in the org.fulib.tables package,
or provide a public constructor with one parameter of type Table.
TAB - the result typetype - the Class corresponding to <TAB>.public <U> Table<U> expand(java.lang.String columnName, java.util.function.Function<? super T,? extends U> function)
Example:
Table<Integer> a = new Table<>("A", 1, 2, 3);
Table<Integer> b = a.expand("B", i -> i * 2);
| A | B |
|---|---|
| 1 | 2 |
| 2 | 4 |
| 3 | 6 |
U - the cell type of the new columncolumnName - the name of the new columnfunction - the function that computes a value for the new columnpublic <U> Table<U> expandAll(java.lang.String columnName, java.util.function.Function<? super T,? extends java.util.Collection<? extends U>> function)
Example:
Table<Integer> a = new Table<>("A", 1, 2);
Table<Integer> b = a.expandAll("B", i -> Arrays.asList(i + 10, i + 20));
| A | B |
|---|---|
| 1 | 11 |
| 1 | 21 |
| 2 | 12 |
| 2 | 22 |
U - the cell type of the new columncolumnName - the name of the new columnfunction - the function that computes a collection of values for the new columnpublic <U> Table<U> derive(java.lang.String columnName, java.util.function.Function<? super java.util.Map<java.lang.String,java.lang.Object>,? extends U> function)
Example:
Table<Integer> a = new Table<>("A", 1, 2);
Table<Integer> b = a.expand("B", i -> i * 10);
Table<Integer> c = b.derive("C", row -> (int) row.get("A") + (int) row.get("B"));
| A | B | C |
|---|---|---|
| 1 | 10 | 11 |
| 2 | 20 | 22 |
U - the cell type of the new columncolumnName - the name of the new columnfunction - the function that computes a value for the new columnpublic <U> Table<U> deriveAll(java.lang.String columnName, java.util.function.Function<? super java.util.Map<java.lang.String,java.lang.Object>,? extends java.util.Collection<? extends U>> function)
Example:
Table<Integer> a = new Table<>("A", 1, 2);
Table<Integer> b = a.expand("B", i -> i * 10);
Table<Integer> c = b.deriveAll("C", row -> {
int a1 = (int) row.get("A");
int b1 = (int) row.get("B");
return Arrays.asList(a1 + b1, a1 * b1);
});
| A | B | C |
|---|---|---|
| 1 | 10 | 11 |
| 1 | 10 | 10 |
| 2 | 20 | 22 |
| 2 | 20 | 40 |
U - the cell type of the new columncolumnName - the name of the new columnfunction - the function that computes a collection of values for the new columnpublic Table<T> dropColumns(java.lang.String... columnNames)
After this operation, table instances with the same underlying data that pointed to one of these columns will throw an exception when operated on in any way that accesses their corresponding column.
Example:
Table<String> names = new StringTable("Alice", "Bob", "Charlie", "alice");
Table<String> uppercase = names.expand("uppercase", String::toUpperCase);
Table<String> lowercase = names.expand("lowercase", String::toLowerCase);
| A | uppercase | lowercase |
|---|---|---|
| Alice | ALICE | alice |
| Bob | BOB | bob |
| Charlie | CHARLIE | charlie |
| alice | ALICE | alice |
names.dropColumns(names.getColumnName());
| uppercase | lowercase |
|---|---|
| ALICE | alice |
| BOB | bob |
| CHARLIE | charlie |
names.toList(); // throws IllegalStateException
columnNames - the names of columns to droppublic Table<T> selectColumns(java.lang.String... columnNames)
IllegalArgumentException is
thrown and no changes are made. Note the original order of columns is unaffected by the order of the given names.
After this operation, table instances with the same underlying data that pointed to a columns not part of the given ones will throw an exception when operated on in any way that accesses their corresponding column.
Example:
Table<String> names = new StringTable("Alice", "Bob", "Charlie", "alice");
Table<String> uppercase = names.expand("uppercase", String::toUpperCase);
Table<String> lowercase = names.expand("lowercase", String::toLowerCase);
| A | uppercase | lowercase |
|---|---|---|
| Alice | ALICE | alice |
| Bob | BOB | bob |
| Charlie | CHARLIE | charlie |
| alice | ALICE | alice |
names.selectColumns("uppercase", "lowercase");
| uppercase | lowercase |
|---|---|
| ALICE | alice |
| BOB | bob |
| CHARLIE | charlie |
names.toList(); // throws IllegalStateException
columnNames - the names of columns to retainjava.lang.IllegalArgumentException - when columnNames contains a column name that is not part of this tablepublic Table<T> filter(java.util.function.Predicate<? super T> predicate)
false when passed the cell value of the
column this table points to.
Example:
final Table<Integer> numbers = new Table<>("A", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
numbers.filter(i -> i % 2 == 0);
| A |
|---|
| 2 |
| 4 |
| 6 |
| 8 |
| 10 |
predicate - the predicate that determines which rows should be keptpublic Table<T> filterRows(java.util.function.Predicate<? super java.util.Map<java.lang.String,java.lang.Object>> predicate)
false.
The rows are passed as maps from column name to cell value.
Example:
Table<Integer> a = new Table<>("A", 1, 2);
Table<Integer> b = a.expandAll("B", i -> Arrays.asList(1, 2));
| A | B |
|---|---|
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
a.filterRows(row -> (int) row.get("A") != (int) row.get("B"));
| A | B |
|---|---|
| 1 | 2 |
| 2 | 1 |
predicate - the predicate that determines which rows should be keptpublic int rowCount()
public java.util.Iterator<T> iterator()
iterator in interface java.lang.Iterable<T>public java.util.List<T> toList()
public java.util.Set<T> toSet()
public java.util.stream.Stream<T> stream()
public java.lang.String toString()
toString in class java.lang.Object@Deprecated public java.util.ArrayList<java.util.ArrayList<java.lang.Object>> getTable()
@Deprecated public java.util.LinkedHashMap<java.lang.String,java.lang.Integer> getColumnMap()