0

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.

wouterio
  • 137
  • 1
  • 10

1 Answers1

0

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

Scott Conway
  • 975
  • 7
  • 13
  • Thanks for your response. IterableCSVToBean and its Builder seem to be deprecated though, so for now I'll just stick with parsing all at once. – wouterio Jun 16 '18 at 07:18
  • Sorry my bad. I should have checked the JavaDocs. In CsvToBean call the iterator method and then convert the iterator to stream. Disclaimer: I am the project keeper of opencsv and I wrote the IterableCSVToBean, which is why it popped into my mind. Andrew Jones updated CsvToBean to return an iterator which made the IterableCSVToBean redundant - which is why it is deprecated. Hope that helps. :) – Scott Conway Jun 17 '18 at 01:56
  • No worries, thanks for keeping OpenCSV in the first place. (And a bit of topic, but if you're the project keeper, would it be worth replacing interface CsvToBeanFilter with the standard Predicate?) – wouterio Jun 18 '18 at 08:20
  • I cannot. Currently OpenCSV requires Java 7 and Predicates are Java 8. I made the mistake with 3.0 to jump java versions and it turned out to be too soon so I have a "Apache Commons rule" where I check a couple of Apache commons projects (lang, collections) and when they jump java versions I can consider jumping versions. :) – Scott Conway Jun 19 '18 at 01:08