Class CSVRecord
- java.lang.Object
-
- org.apache.commons.csv.CSVRecord
-
- All Implemented Interfaces:
Serializable
,Iterable<String>
public final class CSVRecord extends Object implements Serializable, Iterable<String>
A CSV record parsed from a CSV file.Note: Support for
Serializable
is scheduled to be removed in version 2.0. In version 1.8 the mapping between the column header and the column index was removed from the serialised state. The class maintains serialization compatibility with versions pre-1.8 for the record values; these must be accessed by index following deserialization. There will be loss of any functionally linked to the header mapping when transferring serialised forms pre-1.8 to 1.8 and vice versa.- See Also:
- Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
get(int i)
Returns a value by index.String
get(Enum<?> e)
Returns a value byEnum
.String
get(String name)
Returns a value by name.long
getCharacterPosition()
Returns the start position of this record as a character position in the source stream.String
getComment()
Returns the comment for this record, if any.CSVParser
getParser()
Returns the parser.long
getRecordNumber()
Returns the number of this record in the parsed CSV file.boolean
hasComment()
Checks whether this record has a comment, false otherwise.boolean
isConsistent()
Tells whether the record size matches the header size.boolean
isMapped(String name)
Checks whether a given column is mapped, i.e.boolean
isSet(int index)
Checks whether a column with given index has a value.boolean
isSet(String name)
Checks whether a given columns is mapped and has a value.Iterator<String>
iterator()
Returns an iterator over the values of this record.<M extends Map<String,String>>
MputIn(M map)
Puts all values of this record into the given Map.int
size()
Returns the number of values in this record.Stream<String>
stream()
Returns a sequential ordered stream whose elements are the values.List<String>
toList()
Converts the values to a List.Map<String,String>
toMap()
Copies this record into a new Map of header name to record value.String
toString()
Returns a string representation of the contents of this record.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
get
public String get(Enum<?> e)
Returns a value byEnum
.- Parameters:
e
- an enum- Returns:
- the String at the given enum String
-
get
public String get(int i)
Returns a value by index.- Parameters:
i
- a column index (0-based)- Returns:
- the String at the given index
-
get
public String get(String name)
Returns a value by name.Note: This requires a field mapping obtained from the original parser. A check using
isMapped(String)
should be used to determine if a mapping exists from the providedname
to a field index. In this case an exception will only be thrown if the record does not contain a field corresponding to the mapping, that is the record length is not consistent with the mapping size.- Parameters:
name
- the name of the column to be retrieved.- Returns:
- the column value, maybe null depending on
CSVFormat.getNullString()
. - Throws:
IllegalStateException
- if no header mapping was providedIllegalArgumentException
- ifname
is not mapped or if the record is inconsistent- See Also:
isMapped(String)
,isConsistent()
,getParser()
,CSVFormat.Builder.setNullString(String)
-
getCharacterPosition
public long getCharacterPosition()
Returns the start position of this record as a character position in the source stream. This may or may not correspond to the byte position depending on the character set.- Returns:
- the position of this record in the source stream.
-
getComment
public String getComment()
Returns the comment for this record, if any. Note that comments are attached to the following record. If there is no following record (i.e. the comment is at EOF) the comment will be ignored.- Returns:
- the comment for this record, or null if no comment for this record is available.
-
getParser
public CSVParser getParser()
Returns the parser.Note: The parser is not part of the serialized state of the record. A null check should be used when the record may have originated from a serialized form.
- Returns:
- the parser.
- Since:
- 1.7
-
getRecordNumber
public long getRecordNumber()
Returns the number of this record in the parsed CSV file.ATTENTION: If your CSV input has multi-line values, the returned number does not correspond to the current line number of the parser that created this record.
- Returns:
- the number of this record.
- See Also:
CSVParser.getCurrentLineNumber()
-
hasComment
public boolean hasComment()
Checks whether this record has a comment, false otherwise. Note that comments are attached to the following record. If there is no following record (i.e. the comment is at EOF) the comment will be ignored.- Returns:
- true if this record has a comment, false otherwise
- Since:
- 1.3
-
isConsistent
public boolean isConsistent()
Tells whether the record size matches the header size.Returns true if the sizes for this record match and false if not. Some programs can export files that fail this test but still produce parsable files.
- Returns:
- true of this record is valid, false if not
-
isMapped
public boolean isMapped(String name)
Checks whether a given column is mapped, i.e. its name has been defined to the parser.- Parameters:
name
- the name of the column to be retrieved.- Returns:
- whether a given column is mapped.
-
isSet
public boolean isSet(int index)
Checks whether a column with given index has a value.- Parameters:
index
- a column index (0-based)- Returns:
- whether a column with given index has a value
-
isSet
public boolean isSet(String name)
Checks whether a given columns is mapped and has a value.- Parameters:
name
- the name of the column to be retrieved.- Returns:
- whether a given columns is mapped and has a value
-
putIn
public <M extends Map<String,String>> M putIn(M map)
Puts all values of this record into the given Map.- Type Parameters:
M
- the map type- Parameters:
map
- The Map to populate.- Returns:
- the given map.
- Since:
- 1.9.0
-
size
public int size()
Returns the number of values in this record.- Returns:
- the number of values.
-
stream
public Stream<String> stream()
Returns a sequential ordered stream whose elements are the values.- Returns:
- the new stream.
- Since:
- 1.9.0
-
toMap
public Map<String,String> toMap()
Copies this record into a new Map of header name to record value.- Returns:
- A new Map. The map is empty if the record has no headers.
-
toString
public String toString()
Returns a string representation of the contents of this record. The result is constructed by comment, mapping, recordNumber and by passing the internal values array toArrays.toString(Object[])
.
-
-