There is another alternative, we might replace the records returned as a String[] by a CSVRecord class able to access the fields by id or by name. This would be similar to a JDBC resultset (except for the looping logic)

This avoids the duplication of the parser, which might still be generified later to support custom beans.

The example becomes:

  CSVFormat format = CSVFormat.DEFAULT.withHeader();

  for (CSVRecord record : format.parse(in)) {
      Person person = new Person();
      person.setName(record.get("name"));
      person.setEmail(record.get("email"));
      person.setPhone(record.get("phone"));
      persons.add(person);
  }

The record is not a Map to keep it simple, it only exposes 3 methods: get(int), get(String) and size()

Emmanuel Bourg


Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to