简体   繁体   中英

Create CSV file with columns and values from HashMap

Be gentle, This is my first time using Apache Commons CSV 1.7.

I am creating a service to process some CSV inputs, add some additional information from exterior sources, then write out this CSV for ingestion into another system.

I store the information that I have gathered into a list of HashMap<String, String> for each row of the final output csv. The Hashmap contains the <ColumnName, Value for column> .

I have issues using the CSVPrinter to correctly assign the values of the HashMaps into the rows. I can concatenate the values into a string with commas between the variables; however, this just inserts the whole string into the first column.

I cannot define or hardcode the headers since they are obtained from a config file and may change depending on which project uses the service.

Here is some of my code:

try (BufferedWriter writer = Files.newBufferedWriter(
    Paths.get(OUTPUT + "/" + project + "/" + project + ".csv"));)
{
    CSVPrinter csvPrinter = new CSVPrinter(writer,
        CSVFormat.RFC4180.withFirstRecordAsHeader());
    csvPrinter.printRecord(columnList);

for (HashMap<String, String> row : rowCollection)
{
    //Need to map __record__ to column -> row.key, value -> row.value for whole map.

    csvPrinter.printrecord(__record__);
}

csvPrinter.flush();

}

Thanks for your assistance.

You actually have multiple concerns with your technique;

  1. How do you maintain column order?
  2. How do you print the column names?
  3. How do you print the column values?

Here are my suggestions.

  1. Maintain column order. Do not use HashMap , because it is unordered. Instead, use LinkedHashMap which has a "predictable iteration order" (ie maintains order).

  2. Print column names. Every row in your list contains the column names in the form of key values, but you only print the column names as the first row of output. The solution is to print the column names before you loop through the rows. Get them from the first element of the list.

  3. Print column values. The "billal GHILAS" answer demonstrates a way to print the values of each row.

Here is some code:

try (BufferedWriter writer = Files.newBufferedWriter(
     Paths.get(OUTPUT + "/" + project + "/" + project + ".csv"));)
{
    CSVPrinter csvPrinter = new CSVPrinter(writer,
        CSVFormat.RFC4180.withFirstRecordAsHeader());

    // This assumes that the rowCollection will never be empty.
    // An anonymous scope block just to limit the scope of the variable names.
    {
        HashMap<String, String> firstRow = rowCollection.get(0);
        int valueIndex = 0;
        String[] valueArray = new String[firstRow.size()];

        for (String currentValue : firstRow.values())
        {
            valueArray[valueIndex++] = currentValue;
        }

        csvPrinter.printrecord(valueArray);
    }

    for (HashMap<String, String> row : rowCollection)
    {
        int valueIndex = 0;
        String[] valueArray = new String[row.size()];

        for (String currentValue : row.values())
        {
            valueArray[valueIndex++] = currentValue;
        }

        csvPrinter.printrecord(valueArray);
    }

    csvPrinter.flush();
}
for (HashMap<String,String> row : rowCollection) {
       Object[] record = new Object[row.size()];
       for (int i = 0; i < columnList.size(); i++) {
            record[i] = row.get(columnList.get(i));
       }

       csvPrinter.printRecord(record);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM