I have very large csv files that I'm trying to iterate through. I'm using opencsv and I'd like to use CsvToBean so that I can dynamically set the column mappings from a database. The question I have is how to do this without grabbing the entire file and throwing it into a list. I'm trying to prevent memory errors.
I'm currently passing the entire result set into a list like so.
List<MyOption> myObjects = csv.parse(strat, getReader("file.txt"));
for (MyObject myObject : myObjects) {
System.out.println(myObject);
}
But I found this iterator method and I'm wondering if this will just iterate each row rather than the entire file at once?
Iterator myObjects = csv.parse(strat, getReader("file.txt")).iterator();
while (myObjects.hasNext()) {
MyObject myObject = (MyObject) myObjects.next();
System.out.println(myObject);
}
So my question is what is the difference between Iterator and list?