1

I am using the last version of common-csv library, such as in my pom.xml I have this dependency:

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-csv</artifactId>
  <version>1.7</version>
</dependency>

This library is used in order to write a simple CSV file in a java application. In particular use cases the column name headers of the csv file can be duplicated. I found an interesting property of the CSVFormat class that must be useful in this case but, in every solution described below, the program terminates with error such as:

Exception in thread "main" java.lang.IllegalArgumentException: 
The header contains a duplicate entry: 'VV' in [CC, VV, VV]
  at org.apache.commons.csv.CSVFormat.validate(CSVFormat.java:1676)
  at org.apache.commons.csv.CSVFormat.<init>(CSVFormat.java:793)
  at org.apache.commons.csv.CSVFormat.withHeader(CSVFormat.java:1986)

The code written is:

public static void main(String[] args){
    CSVFormat formatCsv = CSVFormat.DEFAULT.withAllowDuplicateHeaderNames()
                                           .withHeader("CC","VV","VV");
    System.out.println(formatCsv);
}

I have already tried 4 situations:

CSVFormat formatCsv = CSVFormat.DEFAULT.withAllowDuplicateHeaderNames()
                                       .withHeader(headers);

CSVFormat formatCsv = CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(true)
                                       .withHeader(headers);

CSVFormat formatCsv = CSVFormat.DEFAULT.withHeader(headers)
                                       .withAllowDuplicateHeaderNames();

CSVFormat formatCsv = CSVFormat.DEFAULT.withHeader(headers)
                                       .withAllowDuplicateHeaderNames(true);

Is there a bug for the withAllowDuplicateHeaderNames property? It is very difficult to rewrite the code source of the library to change the validation method of CSVFormat.class

2
  • 1
    withAllowDuplicateHeaderNames() currently only affects headers read from the data itself, not headers that you specify via withHeader(), those are always checked for duplicates. But you should be able to filter out those duplicates from the header-collection which you provide easily, or? Commented Sep 8, 2019 at 14:36
  • @centic I suggest making an Answer of your Comment, so this Question can be marked resolved. Commented Sep 8, 2019 at 17:32

1 Answer 1

3

According to the sources of version 1.7, withAllowDuplicateHeaderNames() currently only affects headers read from the data itself, not headers that you specify via withHeader(), the headers that you specify are currently always checked for duplicates.

This was fixed in version 1.8, see also CSV-241 and PR #43.

A workaround if you are using an older version would be to to filter out those duplicates from the header-collection which you provide.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answer. When you write "A workaround for now would be to to filter out those duplicates from the header-collection which you provide." what do you mean? Regards.
When you say you call it with ".withHeader(headers);", the "headers" is some sort of collection that you provide. It should be possible to filter duplicates from this collection before you give it to withHeader(), or?
Hi, I have not to filter duplicated but the headers must be written in the order of the items of the headers list.
Thanks for the answer. The update 1.8 has the fix for withAllowDuplicateHeaderNames
Thanks for the note, I have adjusted the answer for actual release of version 1.8 now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.