I'm trying to map a Csv file to an Object what instances List of Objects using CsvMapper, is that even possible?
Example:
Content of Csv file (without header):
Field 1: name
Field 2: gender
Field 3: hobby (List comma separated)
Field 4: relation
Bob;male;riding,swimming,dance;single
Example class:
@AllArgsConstructor(access = AccessLevel.PRIVATE)
Class User {
String name;
String gender;
List<Hobby> hobbys;
String relation;
Boolean check;
int index;
public static User from(String name,
String gender,
List<Hobby> hobbys,
String relation) {
------ SOME LOGIC -----
return new User(name,
gender,
hobbys,
relation,
index)
}
}
Class UserCreator{
public List<User> create(File csvFile){
//Now my Problem how can I map the csvFile to User Object???
//my attempts
CsvMapper csvMapper = new CsvMapper();
CsvSchema csvSchema = CsvSchema
.emptySchema()
.withoutHeader()
.withColumnSeparator(',')
.withLineSeparator(";");
try {
//here its crashes, my problem ist how can I map it to the User object?
MappingIterator<Map<String, String>> it = csvMapper.readerFor(Map.class)
.with(csvSchema)
.readValues(file);
System.out.println("test");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thank you in advance.