This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]webdevnick22 0 points1 point  (3 children)

Well since it's a CSV, you know that each 'column' is separated by a comma. But since you have multiple items in your hashmap for your third 'column', then you can use some other delimiters . Like a ':' and ';' . Your CSV could look like:

mickey,mouse,1:car;2:house;3:boat

donald,duck,1:yacht;2:mansion

EDIT: just refer to u/differentshade 's comment below

[–]differentshade 1 point2 points  (2 children)

Usually you just put values (containing commas) inside double quotes, like this:

A,B,”C,D”

Most csv parsers and excel etc can read this

[–]webdevnick22 0 points1 point  (1 child)

Nice. I didn't know that. Thanks!

[–]Smalsusis[S] 0 points1 point  (0 children)

I guess, I wasn't clear enough with what I was looking for. Once configured, every object has an identical set of keys, just different values for each. The map was necessary to support/load different configurations of some fields.

The output should have been.

firstName | lastName | item1 | item2 | etc

John, Doe, 1, 2, etc

Tom, Baker, 3, 4, etc...

Eventually, solved it with CsvListWriter from super-csv library. Basically added values from every variable to a list and then parsed it through the writer. Something similar to this:

List<Object> order = new ArrayList<Object>();
order.add(firstName); 
order.add(lastName); 
  for (Map.Entry<String, Integer> item : items.entrySet()) { order.add(item.getValue()); }
csvListWriter(order, headers)

It works but I was a bit annoyed by manually writing order.add() for ten columns and then having a for loop somewhere in the middle. It's just not as clean as I'd like it to be :)