Class 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:
    CsvReader
    • Constructor Summary

      Constructors 
      Constructor Description
      NamedCsvRecord​(long startingLineNumber, java.lang.String[] fields, boolean comment, java.lang.String[] header)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private int commonSize()  
      java.util.Optional<java.lang.String> findField​(java.lang.String name)
      Retrieves the value of a field by its case-sensitive name, considering the first occurrence in case of duplicates.
      java.util.List<java.lang.String> findFields​(java.lang.String name)
      Collects all field values with the given name (case-sensitive) in the order they appear in the header.
      private int findHeaderIndex​(java.lang.String name)  
      java.lang.String getField​(java.lang.String name)
      Retrieves the value of a field by its case-sensitive name, considering the first occurrence in case of duplicates.
      java.util.Map<java.lang.String,​java.lang.String> getFieldsAsMap()
      Constructs an ordered map, associating header names with corresponding field values of this record, considering the first occurrence in case of duplicates.
      java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getFieldsAsMapList()
      Constructs an unordered map, associating header names with an ordered list of corresponding field values in this record.
      java.util.List<java.lang.String> getHeader()
      Retrieves the header names of this record.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • header

        private final java.lang.String[] header
    • Constructor Detail

      • NamedCsvRecord

        NamedCsvRecord​(long startingLineNumber,
                       java.lang.String[] fields,
                       boolean comment,
                       java.lang.String[] header)
    • Method Detail

      • getHeader

        public java.util.List<java.lang.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 java.lang.String getField​(java.lang.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:
        java.util.NoSuchElementException - if this record has no such field
        java.lang.NullPointerException - if name is null
        See Also:
        findField(String), findFields(String)
      • findHeaderIndex

        private int findHeaderIndex​(java.lang.String name)
      • findField

        public java.util.Optional<java.lang.String> findField​(java.lang.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:
        java.lang.NullPointerException - if name is null
        See Also:
        findFields(String)
      • findFields

        public java.util.List<java.lang.String> findFields​(java.lang.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:
        java.lang.NullPointerException - if name is null
      • getFieldsAsMap

        public java.util.Map<java.lang.String,​java.lang.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()
      • getFieldsAsMapList

        public java.util.Map<java.lang.String,​java.util.List<java.lang.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:
        getFieldsAsMap()
      • commonSize

        private int commonSize()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class CsvRecord