Skip to content

Bean Mapping

Many CSV libraries come with built-in support for mapping CSV records to Java beans. While this is a convenient feature, the reflection-based approach used by most libraries comes with a heavy performance penalty, which contradicts FastCSV’s design goal of being fast.

Thanks to Java stream mapping, FastCSV can provide similar functionality without sacrificing performance.

Example

The following example demonstrates how to map CSV records to Java beans using FastCSV.

ExampleCsvReaderMapping.java
package example;
import java.io.IOException;
import java.util.stream.Stream;
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.NamedCsvRecord;
/**
* Example for reading CSV data with a mapping function.
*/
public class ExampleCsvReaderMapping {
private static final String DATA = """
ID,firstName,lastName
1,John,Doe
2,Jane,Smith
""";
public static void main(final String[] args) throws IOException {
try (var persons = readPersons()) {
persons.forEach(System.out::println);
}
}
private static Stream<Person> readPersons() throws IOException {
try (var csv = CsvReader.builder().ofNamedCsvRecord(DATA)) {
return csv.stream().map(ExampleCsvReaderMapping::mapPerson);
}
}
private static Person mapPerson(final NamedCsvRecord rec) {
return new Person(
Long.parseLong(rec.getField("ID")),
rec.getField("firstName"),
rec.getField("lastName")
);
}
private record Person(long id, String firstName, String lastName) {
}
}

You also find this source code example in the FastCSV GitHub repository.