Class NamedCsvRecord

java.lang.Object
de.siegmar.fastcsv.reader.CsvRecord
de.siegmar.fastcsv.reader.NamedCsvRecord

public final class NamedCsvRecord extends CsvRecord
Represents an immutable CSV record with named (and indexed) fields.

The field values are never null. Empty fields are represented as empty strings.

See Also:
  • Field Details

  • Constructor Details

    • NamedCsvRecord

      NamedCsvRecord(long startingLineNumber, String[] fields, boolean comment, String[] header)
  • Method Details

    • getHeader

      public List<String> getHeader()
      Retrieves the header names of this record.

      The header names are returned in the order they appear in the CSV file.

      Note that the header names are not necessarily unique. If you need to collect all fields with the same name (duplicate header), use getFieldsAsMapList().

      Note that records for commented lines (CsvRecord.isComment()) do not have an empty header. To retrieve the comment value, user CsvRecord.getField(int) with index 0.

      Returns:
      the header names, never null
    • getField

      public String getField(String name)
      Retrieves the value of a field by its case-sensitive name, considering the first occurrence in case of duplicates.

      This method is equivalent to findField(name).orElseThrow(NoSuchElementException::new) although a more explanatory exception message is provided.

      Parameters:
      name - case-sensitive name of the field to be retrieved
      Returns:
      field value, never null
      Throws:
      NoSuchElementException - if this record has no such field
      NullPointerException - if name is null
      See Also:
    • findHeaderIndex

      private int findHeaderIndex(String name)
    • findField

      public Optional<String> findField(String name)
      Retrieves the value of a field by its case-sensitive name, considering the first occurrence in case of duplicates.

      This method is equivalent to findFields(name).stream().findFirst() but more performant.

      Parameters:
      name - case-sensitive name of the field to be retrieved
      Returns:
      An Optional containing the value of the field if found, or an empty Optional if the field is not present. Never returns null.
      Throws:
      NullPointerException - if name is null
      See Also:
    • findFields

      public List<String> findFields(String name)
      Collects all field values with the given name (case-sensitive) in the order they appear in the header.
      Parameters:
      name - case-sensitive name of the field to collect values for
      Returns:
      the field values (empty list if record doesn't contain that field), never null
      Throws:
      NullPointerException - if name is null
    • getFieldsAsMap

      public Map<String,String> getFieldsAsMap()
      Constructs an ordered map, associating header names with corresponding field values of this record, considering the first occurrence in case of duplicates.

      The constructed map will only contain entries for fields that have a key and a value. No map entry will have a null key or value.

      If you need to collect all fields with the same name (duplicate header), use getFieldsAsMapList().

      Returns:
      an ordered map of header names and field values of this record, never null
      See Also:
    • getFieldsAsMapList

      public Map<String,List<String>> getFieldsAsMapList()
      Constructs an unordered map, associating header names with an ordered list of corresponding field values in this record.

      The constructed map will only contain entries for fields that have a key and a value. No map entry will have a null key or value.

      If you don't have to handle duplicate headers, you may simply use getFieldsAsMap().

      Returns:
      an unordered map of header names and field values of this record, never null
      See Also:
    • commonSize

      private int commonSize()
    • toString

      public String toString()
      Overrides:
      toString in class CsvRecord