title: Parse CSV to multiple/nested bean types with OpenCSV?
body: <p>I have various CSVs that contain some standard columns and some completely random fields:</p>
  
  <pre><code>firstname, lastname, dog_name, fav_hat, fav_color
  bill,smith,fido,porkpie,blue
  james,smith,rover,bowler,purple
  
  
  firstname, lastname, car_type, floor_number
  tom, collins, ford, 14
  jim, jones, toyota, 120
  </code></pre>
  
  <p>So I'm trying to parse those into Person.class beans, which holds firstname &amp; lastname, then I have a second class called PersonAttribute.class to hold...whatever else.</p>
  
  <p>The bastic outline of the two classes:</p>
  
  <pre><code>class Person {
   public String firstname;
   public String lastname;
   public List&lt;PersonAttribute&gt; attribs;
  }
  
  class PersonAttribute {
   public Person p;
   public String key; // header name, ex. 'car_type'
   public String value; // column value, ex. 'ford'
  }
  </code></pre>
  
  <p>I've been using the CsvToBean functions in opencsv:</p>
  
  <pre><code>public static List&lt;Person&gt; parseToBeans(File csvFile, HashMap&lt;String, String&gt; mapStrategy, Class beanClass) throws IOException {
      CSVReader reader = null;
      try {
          reader = new CSVReader(new BufferedReader(new FileReader(csvFile)));
  
          HeaderColumnNameTranslateMappingStrategy&lt;Person&gt; strategy = new HeaderColumnNameTranslateMappingStrategy&lt;&gt;();
          strategy.setType(beanClass);
          strategy.setColumnMapping(mapStrategy);
  
          final CsvToBean&lt;Person&gt; csv = new CsvToBean&lt;Person&gt;() {
              @Override
              protected Object convertValue(String value, PropertyDescriptor prop) throws InstantiationException, IllegalAccessException {
                  value = value.trim().replaceAll(" +", " ");
                  return super.convertValue(value, prop);
              }
          };
          return csv.parse(strategy, reader);
      }
  ...
  </code></pre>
  
  <p>However I'm not sure how to handle creating PersonAttribute.class beans while I'm parsing the csv for Person.class beans. I came across <a href="https://stackoverflow.com/a/13523698/1107232">this post</a> and am wondering if I need to switch to supercsv to easily handle what I'm trying to do?</p>