Skip to content

5 Iterating through the parsed data

Dan Geabunea edited this page Apr 1, 2015 · 1 revision

After reading a CSV document, the next step is to iterate through the information. The CsvDocument class is made up of CsvRow objects. Each CsvRow object is made up of CsvColumn objects. With this in mind, lets dive into some examples.

Reading columns values as String

...
CsvDocument document = CsvDocument.read(csvPath, skipHeaderConfig);
for (CsvRow row : document .getCsvRows()){
    System.out.println("###");
    for(int i=0;i<row.getNumberOfColumns();i++){
        System.out.print(row.getColumnAtIndex(i).getColumnValue());
    }
}

Are all column values represented as String?

No! If the CSV document is made up of numbers, boolean flags, dates you can get those values directly, without having to manually transform them from String.

Let's say we want to parse the following document and access the age as a number, the employed as a boolean flag and the birthday as a date.

Name,Age,Employed,BirthDate
John,25,true,1990-10-10
Anna,30,false,1985-01-01
...
CsvDocument document = CsvDocument.read(csvPath, skipHeaderConfig);
for (CsvRow row : document.getCsvRows()){
    String name = row.getColumnAtIndex(0).getColumnValue();
    int age = row.getColumnAtIndex(1).getInteger();
    boolean isEmployed = row.getColumnAtIndex(2).getBoolean();
    LocalDate birthDate = row.getColumnAtIndex(3).getDate("YYYY-MM-dd");
}

Can I only parse boolean values from "true" or "false"?

No! The getBoolean() method of the CsvColumn class can transform other values to boolean as well.

  • true,True,TRUE,T,yes,Yes,y,Y,1 will be converted to a boolean true value
  • false,False,FALSE,f,F,No,NO,n,N,0 will be converted to a boolean false value

What if I want to map every CSV row to a POJO?

The library does not provide a feature to auto map a CSV row entry into a POJO. However, you can make use of the Java Stream API to accomplish this task with ease.

Let's use the same CSV extract as above.

Name,Age,Employed,BirthDate
John,25,true,1990-10-10
Anna,30,false,1985-01-01

And let us assume that we have a Person java class like this.

class Person {
    private String name;
    private int age;
    private boolean isEmployed;

    public Person(String name, int age, boolean isEmployed){
        this.name = name;
        this.age = age;
        this.isEmployed = isEmployed;
    }

    public String getName() {
       return name;
    }

    public int getAge() {
        return age;
    }

    public boolean isEmployed() {
        return isEmployed;
    }
}

Now let us map each CSV row to a Person object.

...
CsvDocument personDocument= CsvDocument.read(csvPath, skipHeaderConfig);
List<Person> persons = personDocument.getCsvRows().stream()
                .map(x -> new Person(
                        x.getColumnAtIndex(0).getColumnValue(),
                        x.getColumnAtIndex(1).getInteger(),
                        x.getColumnAtIndex(2).getBoolean()
                ))
                .collect(Collectors.toList());
...

Easy, right? You can also make use of the labda expression and add custom filtering as well to the mapping process, thus customizing the import process even more.

Clone this wiki locally