0

let I have a hashmap HashMap. Now I want to write all the values row-wise into a CSV file

HashMap:
Key: BOF   Value: SAPF,754
Key: BOM   Value: SAPM,456
Key: BOL   Value: SAPL,987

I want to make a csv with this format:

SAPF,754
SAPM,456
SAPL,987

How to do it?

2
  • By writing some code, possibly helpeed by one of the dozens of libraries allowing to write CSV files in Java. Do some research, read documentation, and start trying something. Then come back here if you have a concrete question. Commented Sep 20, 2017 at 11:16
  • You are expected to do serious research prior posting questions. It took me less than 3 seconds to identify multiple questions asking the very same thing... Commented Sep 20, 2017 at 11:17

1 Answer 1

2

You can iterate through the entries and write to a file, e.g.:

Map<String, String[]> map = //Map with data
try(BufferedWriter writer = new BufferedWriter(new FileWriter("<your_file>"))){
    for(Map.Entry<String, String[]> entry : map.entrySet()){
        writer.write(String.join(",", entry.getValue()));
        writer.newLine();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Darshan ..thank you so much... writer.write(String.join(",", entry.getValue())); does this mean u r putting ',' as delimiter?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.