toList
Converts a DataFrame into a List of data class instances, one per row, in the order of the rows.
toListOf<T>() creates instances of the T you give in the call, whatever the type argument of the DataFrame is. Use it in most cases. toList() creates instances of the type argument of the DataFrame, so use it only when this type argument is known to be a data class.
Related operations: toDataFrame (the reverse operation), convertTo, toMap.
More info: Interop with Collections
How rows become objects
The rules are the same for toList, toListOf, toSequence and toSequenceOf.
Thas to be a data class. An interface does not work, even one marked with@DataSchema. A generic data class works too, for example,Pair<String, Int>.Each instance is created by the primary constructor of
T. Each constructor parameter gets the value of the column with the same name, or with the name given in@ColumnName. Names are case-sensitive.The order of the columns does not matter, and columns that match no parameter are ignored.
A parameter with a default value gets this value when there is no column for it.
When a column type differs from the parameter type, the values are converted the same way as in
convertTo, for example, fromInttoLong.A
ColumnGroupbecomes a nested data class, and aFrameColumnbecomes aListof data class instances, or aDataFrame. A frame that goes into aDataFrame<S>parameter is converted to the schema ofSthe same way as inconvertTo, and its other columns stay. A frame that goes into aDataFrame<*>parameter is passed as it is.
An exception is thrown when:
Tis not a data class;there is no column for a constructor parameter without a default value;
a column has
nullvalues, but its parameter is not nullable;a value cannot be converted to the parameter type;
a frame cannot be converted to the schema of its
DataFrame<S>parameter, for example, because a column ofSis missing.
toList
toList() creates instances of the type argument of the DataFrame, following the rules in How rows become objects.
Use it only when the type argument of the DataFrame is known to be a data class. On a DataFrame<*>, such as the result of dataFrameOf or of reading a file, toList() throws an exception, because there is no data class to create. With the compiler plugin, the same happens even right after toDataFrame() on a list of data class instances: the plugin gives the result a generated type argument, which is not a data class. In these cases, use toListOf, or cast the DataFrame to a data class first:
Output:
toListOf
toListOf<T>() creates instances of the T given in the call, following the rules in How rows become objects. Use it in most cases. The type argument of the DataFrame does not matter, so it works on a DataFrame<*> too, and on a DataFrame whose type argument is generated by the compiler plugin.
Here the fullName column group becomes a nested FullName:
Output:
toSequence and toSequenceOf
toSequence() and toSequenceOf<T>() are the same as toList() and toListOf<T>(), but they return a Sequence. The rules in How rows become objects apply to them too.
The columns are matched and converted when the function is called, so the exceptions are thrown right away. The instances are created only when the sequence is iterated, one per row that is read, and each iteration creates them again. Here only the first row becomes an object. The dataframes and the data classes are the ones from the toList and toListOf examples:
Output:
Output: