I have a CSV file containing 2 columns (1 containing a list of words, another with the frequency they are used in a text document). I was curious the best way to read that in an ArrayList of type object using SuperCSV. Thanks in advance.
2 Answers
I'm assuming you've read the documentation and examples on the Super CSV website?
If you use CsvListReader
with cell processors (I'd recommend something like new CellProcessor[]{new NotNull(), new NotNull(new ParseInt())}
then you'll get a List of Objects - but you will have to cast to the appropriate type when getting the values out of the List. If you don't use cell processors you'll get a List of Strings and have to convert the count to an Integer- it's up to you but I prefer to let Super CSV to all the conversions.
You can always use CsvBeanReader
to avoid any casting - you'll just have to create a bean with 2 fields: word (String) and count (Integer) and their getters/setters.
It's totally up to you - there really is no 'best way', but using a CSV library is a good practice. In terms of storing the result of the word frequencies, I'd recommend a Map<String,Integer>
(word -> count).

- 9,458
- 4
- 35
- 68
-
I have looked at the site's examples and have the cell processor function but no idea how to use it. – Will Beasley Jun 20 '13 at 12:40
First setup a Processor:
private static CellProcessor[] getProcessors() {
final CellProcessor[] processors = new CellProcessor[] {
new NotNull(new ParseInt()),
new NotNull(),
new NotNull(),
new Optional(new ParseLong())
};
return processors;
}
Next up: Build a reader ( there are 3 types) so for our examples we can use this:
listReader = new CsvListReader(
new InputStreamReader(new FileInputStream(CSVFILE, CHARSET),CsvPreference.TAB_PREFERENCE);
listReader.getHeader(false);
while ((listReader.read(processors)) != null) {}
And you are done, good luck.

- 78
- 10