Top Description Fields Constructors Methods
de.siegmar.fastcsv.reader

public final Class NamedCsvRecord

extends CsvRecord
Class Inheritance
Annotations
@SuppressWarnings:PMD.ArrayIsStoredDirectly
Imports
java.util.ArrayList, .Arrays, .Collections, .HashMap, .LinkedHashMap, .List, .Map, .NoSuchElementException, .Optional, .StringJoiner

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

Field Summary

Modifier and TypeField and Description
private final String[]
Inherited from de.siegmar.fastcsv.reader.CsvRecord:
commentfieldsstartingLineNumber

Constructor Summary

AccessConstructor and Description
pack-priv
NamedCsvRecord(final long startingLineNumber, final String[] fields, final boolean comment, final String[] header)

Method Summary

Modifier and TypeMethod and Description
private int
public Optional<String>

Returns:

An Optional containing the value of the field if found, or an Optional#EMPTY if the field is not present. Never returns null.
findField
(final String
case-sensitive name of the field to be retrieved
name
)

Retrieves the value of a field by its case-sensitive name, considering the first occurrence in case of duplicates.

public List<String>

Returns:

the field values (empty list if record doesn't contain that field), never null
findFields
(final String
case-sensitive name of the field to collect values for
name
)

Collects all field values with the given name (case-sensitive) in the order they appear in the header.

private int
findHeaderIndex(final String name)

public String

Returns:

field value, never null
getField
(final String
case-sensitive name of the field to be retrieved
name
)

Retrieves the value of a field by its case-sensitive name, considering the first occurrence in case of duplicates.

public Map<String, String>

Returns:

an ordered map of header names and field values of this record, never null
getFieldsAsMap
()

Constructs an ordered map, associating header names with corresponding field values of this record, considering the first occurrence in case of duplicates.

public Map<String, List<String>>

Returns:

an unordered map of header names and field values of this record, never null
getFieldsAsMapList
()

Constructs an unordered map, associating header names with an ordered list of corresponding field values in this record.

public List<String>

Returns:

the header names, never null
getHeader
()

Retrieves the header names of this record.

public String
toString()

Overrides de.siegmar.fastcsv.reader.CsvRecord.toString.

Returns a string representation of the object.

Inherited from de.siegmar.fastcsv.reader.CsvRecord:
getFieldgetFieldCountgetFieldsgetStartingLineNumberisComment

Field Detail

Constructor Detail

NamedCsvRecordback to summary
pack-priv NamedCsvRecord(final long startingLineNumber, final String[] fields, final boolean comment, final String[] header)
Annotations
@SuppressWarnings:PMD.UseVarargs

Method Detail

commonSizeback to summary
private int commonSize()
findFieldback to summary
public Optional<String> findField(final 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:String

case-sensitive name of the field to be retrieved

Returns:Optional<String>

An Optional containing the value of the field if found, or an Optional#EMPTY if the field is not present. Never returns null.

Exceptions
NullPointerException:
if name is null
See Also
findFields(String)
findFieldsback to summary
public List<String> findFields(final String name)

Collects all field values with the given name (case-sensitive) in the order they appear in the header.

Parameters
name:String

case-sensitive name of the field to collect values for

Returns:List<String>

the field values (empty list if record doesn't contain that field), never null

Exceptions
NullPointerException:
if name is null
findHeaderIndexback to summary
private int findHeaderIndex(final String name)
getFieldback to summary
public String getField(final 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:String

case-sensitive name of the field to be retrieved

Returns:String

field value, never null

Exceptions
NoSuchElementException:
if this record has no such field
NullPointerException:
if name is null
See Also
findField(String), findFields(String)
getFieldsAsMapback to summary
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:Map<String, String>

an ordered map of header names and field values of this record, never null

Annotations
@SuppressWarnings:PMD.UseConcurrentHashMap
See Also
getFieldsAsMapList()
getFieldsAsMapListback to summary
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:Map<String, List<String>>

an unordered map of header names and field values of this record, never null

Annotations
@SuppressWarnings:PMD.AvoidInstantiatingObjectsInLoops, PMD.UseConcurrentHashMap
See Also
getFieldsAsMap()
getHeaderback to summary
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 (isComment()) do not have an empty header. To retrieve the comment value, user getField(int) with index 0.

Returns:List<String>

the header names, never null

toStringback to summary
public String toString()

Overrides de.siegmar.fastcsv.reader.CsvRecord.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override