File tree 3 files changed +61
-30
lines changed
example/src/main/java/example
3 files changed +61
-30
lines changed Original file line number Diff line number Diff line change @@ -121,36 +121,7 @@ See also: https://github.com/osiegmar/FastCSV/issues/103
121
121
122
122
#### Does FastCSV support automatic bean mapping?
123
123
124
- The goals of FastCSV are not going hand in hand with automatic bean mapping as it would increase the complexity and
125
- footprint of the library and decrease its performance.
126
-
127
- If FastCSV otherwise suits your needs, you can easily map CSV records to beans using the Java stream API.
128
-
129
- ``` java
130
- public class Test {
131
-
132
- public static void main (String [] args ) throws IOException {
133
- var file = Paths . get(" input.csv" );
134
- try (var csv = CsvReader . builder(). ofNamedCsvRecord(file)) {
135
- csv. stream()
136
- .map(Test :: mapPerson)
137
- .forEach(System . out:: println);
138
- }
139
- }
140
-
141
- private static Person mapPerson (NamedCsvRecord rec ) {
142
- return new Person (
143
- Long . parseLong(rec. getField(" ID" )),
144
- rec. getField(" firstName" ),
145
- rec. getField(" lastName" )
146
- );
147
- }
148
-
149
- private record Person (Long id , String firstName , String lastName ) {
150
- }
151
-
152
- }
153
- ```
124
+ See [ Bean Mapping example] ( /guides/examples/bean-mapping/ ) for more information.
154
125
155
126
#### Does FastCSV support automatic type conversion?
156
127
Original file line number Diff line number Diff line change
1
+ ---
2
+ title : Bean Mapping
3
+ ---
4
+
5
+ import SourceExample from ' ../../../../components/SourceExample.astro' ;
6
+
7
+ Many CSV libraries come with built-in support for mapping CSV records to Java beans.
8
+ While this is a convenient feature, the reflection-based approach used by most libraries comes with
9
+ a heavy performance penalty, which contradicts FastCSV’s design goal of being fast.
10
+
11
+ Thanks to Java stream mapping, FastCSV can provide a similar feature without sacrificing performance.
12
+
13
+ ## Example
14
+
15
+ The following example demonstrates how to map CSV records to Java beans using FastCSV.
16
+
17
+ <SourceExample filename = " ExampleCsvReaderMapping.java" />
Original file line number Diff line number Diff line change
1
+ package example ;
2
+
3
+ import java .io .IOException ;
4
+ import java .util .stream .Stream ;
5
+
6
+ import de .siegmar .fastcsv .reader .CsvReader ;
7
+ import de .siegmar .fastcsv .reader .NamedCsvRecord ;
8
+
9
+ /**
10
+ * Example for reading CSV data with a mapping function.
11
+ */
12
+ public class ExampleCsvReaderMapping {
13
+
14
+ private static final String DATA = """
15
+ ID,firstName,lastName
16
+ 1,John,Doe
17
+ 2,Jane,Smith
18
+ """ ;
19
+
20
+ public static void main (final String [] args ) throws IOException {
21
+ try (var persons = readPersons ()) {
22
+ persons .forEach (System .out ::println );
23
+ }
24
+ }
25
+
26
+ private static Stream <Person > readPersons () throws IOException {
27
+ try (var csv = CsvReader .builder ().ofNamedCsvRecord (DATA )) {
28
+ return csv .stream ().map (ExampleCsvReaderMapping ::mapPerson );
29
+ }
30
+ }
31
+
32
+ private static Person mapPerson (final NamedCsvRecord rec ) {
33
+ return new Person (
34
+ Long .parseLong (rec .getField ("ID" )),
35
+ rec .getField ("firstName" ),
36
+ rec .getField ("lastName" )
37
+ );
38
+ }
39
+
40
+ private record Person (long id , String firstName , String lastName ) {
41
+ }
42
+
43
+ }
You can’t perform that action at this time.
0 commit comments