Search code examples
javastreamopencsv

Stream created from CsvtoBean finishes after single element


I'm using OpenCSV to read csv-files and would like to process the data with Streams. The OpenCSV website states however that they 'won't be using Java 8 for many years to come.' So I guess need to fix the Stream part myself.

I can have OpenCSV parse the whole csv-file to a List at once, and then create a Stream from this List. This works fine, but I rather have OpenCSV parse a single line when the Stream actually needs it. So I thought I'd use:

CsvToBean<DataElement> elements = new CsvToBeanBuilder<DataElement>(...).withType(DataElement.class).build();
Stream<DataElement> s = StreamSupport.stream(elements.spliterator(), false);

Where CsvToBean implements Iterable. But the Stream always finishes after just one item. I can't really figure out why. Does someone know?

Thanks in advance.


Solution

  • Try using the IterableCSVToBean instead and then see if you can convert the iterator into a Stream in your Java 8 code.