Skip to content

Commit 282f211

Browse files
author
Gary Gregory
committed
[CSV-239] Cannot get headers in column order from CSVRecord.
- Redo header names as an read-only list.
1 parent 476e883 commit 282f211

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.nio.file.Path;
3434
import java.util.ArrayList;
3535
import java.util.Arrays;
36+
import java.util.Collections;
3637
import java.util.Iterator;
3738
import java.util.List;
3839
import java.util.Map;
@@ -183,9 +184,13 @@ public void remove() {
183184
}
184185

185186
static List<String> createHeaderNames(final Map<String, Integer> headerMap) {
187+
// @formatter:off
186188
return headerMap == null ? null
187-
: headerMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).map(Map.Entry::getKey)
188-
.collect(Collectors.toList());
189+
: headerMap.entrySet().stream()
190+
.sorted(Map.Entry.comparingByValue())
191+
.map(Map.Entry::getKey)
192+
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
193+
// @formatter:on
189194
}
190195

191196
/**
@@ -543,13 +548,13 @@ Map<String, Integer> getHeaderMapRaw() {
543548
}
544549

545550
/**
546-
* Returns a copy of the header names that iterates in column order.
551+
* Returns a read-only list of header names that iterates in column order.
547552
*
548-
* @return a copy of the header names that iterates in column order.
553+
* @return read-only list of header names that iterates in column order.
549554
* @since 1.7
550555
*/
551556
public List<String> getHeaderNames() {
552-
return new ArrayList<>(headerNames);
557+
return headerNames;
553558
}
554559

555560
/**

0 commit comments

Comments
 (0)