Skip to content

Commit bc61b75

Browse files
authored
Merge pull request #309 from SethFalco/header-docs
(doc): Document duplicate header behavior
2 parents f4274a8 + 7f33501 commit bc61b75

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/main/java/org/apache/commons/csv/CSVRecord.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public String get(final int i) {
8888
}
8989

9090
/**
91-
* Returns a value by name.
91+
* Returns a value by name. If multiple instances of the header name exists, only the last occurence is returned.
9292
*
9393
* <p>
9494
* Note: This requires a field mapping obtained from the original parser.
@@ -311,7 +311,9 @@ public List<String> toList() {
311311
}
312312

313313
/**
314-
* Copies this record into a new Map of header name to record value.
314+
* Copies this record into a new Map of header name to record value. If multiple instances of a header name exists,
315+
* only the last occurence is mapped.
316+
*
315317
* <p>
316318
* Editing the map does not update this instance.
317319
* </p>

src/test/java/org/apache/commons/csv/CSVRecordTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.junit.jupiter.api.Assertions.assertNull;
2424
import static org.junit.jupiter.api.Assertions.assertThrows;
2525
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
import static org.junit.jupiter.api.Assertions.assertAll;
2627

2728
import java.io.ByteArrayInputStream;
2829
import java.io.ByteArrayOutputStream;
@@ -341,6 +342,37 @@ public void testToString() {
341342
assertTrue(recordWithHeader.toString().contains("values="));
342343
}
343344

345+
@Test
346+
public void testDuplicateHeaderGet() throws IOException {
347+
final String csv = "A,A,B,B\n1,2,5,6\n";
348+
final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().build();
349+
350+
try (final CSVParser parser = CSVParser.parse(csv, format)) {
351+
final CSVRecord record = parser.nextRecord();
352+
353+
assertAll("Test that it gets the last instance of a column when there are duplicate headings",
354+
() -> assertEquals("2", record.get("A")),
355+
() -> assertEquals("6", record.get("B"))
356+
);
357+
}
358+
}
359+
360+
@Test
361+
public void testDuplicateHeaderToMap() throws IOException {
362+
final String csv = "A,A,B,B\n1,2,5,6\n";
363+
final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().build();
364+
365+
try (final CSVParser parser = CSVParser.parse(csv, format)) {
366+
final CSVRecord record = parser.nextRecord();
367+
final Map<String, String> map = record.toMap();
368+
369+
assertAll("Test that it gets the last instance of a column when there are duplicate headings",
370+
() -> assertEquals("2", map.get("A")),
371+
() -> assertEquals("6", map.get("B"))
372+
);
373+
}
374+
}
375+
344376
private void validateMap(final Map<String, String> map, final boolean allowsNulls) {
345377
assertTrue(map.containsKey(EnumHeader.FIRST.name()));
346378
assertTrue(map.containsKey(EnumHeader.SECOND.name()));

0 commit comments

Comments
 (0)