-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
61 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
title: Bean Mapping | ||
--- | ||
|
||
import SourceExample from '../../../../components/SourceExample.astro'; | ||
|
||
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 a similar feature without sacrificing performance. | ||
|
||
## Example | ||
|
||
The following example demonstrates how to map CSV records to Java beans using FastCSV. | ||
|
||
<SourceExample filename="ExampleCsvReaderMapping.java"/> |
43 changes: 43 additions & 0 deletions
43
example/src/main/java/example/ExampleCsvReaderMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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) { | ||
} | ||
|
||
} |