title: Apache CSV - Convert List<String> to CSVRecord
body: <p>I'm inclined to use <a href="https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVRecord.html" rel="nofollow noreferrer">CSVRecord</a> because it can be used to map with a header and get the corresponding value. My application frequently uses CSVRecord class. However, I cannot instantiate the CSVRecord. I would prefer not to modify the source/create a new class since it already provides a parser that returns CSVRecord. I have got a list of strings (header as well as the values) that needed to be converted to the CSVRecord type. Is there a direct way that this can be done without going around with formatting and then parsing back? Like the one below:</p>
  
  <pre><code>private CSVRecord format(List&lt;String&gt; header, List&lt;String&gt; values)
  {
      CSVFormat csvFormat = CSVFormat.DEFAULT.withRecordSeparator(System.lineSeparator())
              .withQuoteMode(QuoteMode.ALL);
      CSVRecord csvRecord = null;
      final StringWriter out = new StringWriter();
      try (CSVPrinter csvPrinter = new CSVPrinter(out, csvFormat);)
      {
          csvPrinter.printRecord(values);
          String value = out.toString().trim();
          for (CSVRecord r : CSVParser.parse(value, csvFormat.withHeader(header.toArray(new String[header.size()]))))
              csvRecord = r;
      }
      catch (IOException e)
      {
          logger.error("Unable to format the Iterable to CSVRecord. Header: [{}]; Values: [{}]", e,
                  String.join(", ", header), String.join(", ", values));
      }
      return csvRecord;
  }
  
  private void testMethod() throws Exception
  {
      List&lt;String&gt; header = Arrays.asList("header1", "header2", "header3");
      List&lt;String&gt; record = Arrays.asList("val1", "val2", "val3");
      CSVRecord csvRecord = format(header, record);
      logger.info("{}", csvRecord.get("header2"));
  }
  </code></pre>