private static String[] getHeader(
     List<? extends Instance<AttributesMap, Serializable>> instances) {
   Set<String> headerSet = Sets.newTreeSet();
   for (Instance<AttributesMap, Serializable> instance : instances) {
     for (String key : instance.getAttributes().keySet()) {
       headerSet.add(key);
     }
   }
   headerSet.add("label");
   headerSet.add("weight");
   return headerSet.toArray(new String[headerSet.size()]);
 }
 private static String[] instanceToRow(
     Instance<AttributesMap, Serializable> instance, String[] header) {
   String[] row = new String[header.length];
   for (int i = 0; i < header.length; i++) {
     if (((String) header[i]).equals("label")) {
       row[i] = instance.getLabel().toString();
     } else if (((String) header[i]).equals("weight")) {
       row[i] = Double.toString(instance.getWeight());
     } else {
       row[i] =
           instance.getAttributes().containsKey(header[i])
               ? instance.getAttributes().get(header[i]).toString()
               : "";
     }
   }
   return row;
 }