0

I have a countries Map with the following design:

England=24
Spain=21
Italy=10
etc

Then, I have a different citiesMap with the following design:

London=10
Manchester=5
Madrid=7
Barcelona=4
Roma=3
etc

Currently, I am printing these results on screen:

    System.out.println("\nCountries:");
    Map<String, Long> countryMap = countTotalResults(orderDataList, OrderData::getCountry);
    writeResultInCsv(countryMap);
    countryMap.entrySet().stream().forEach(System.out::println);

System.out.println("\nCities:\n");
    Map<String, Long> citiesMap = countTotalResults(orderDataList, OrderData::getCity);
    writeResultInCsv(citiesMap);
    citiesMap.entrySet().stream().forEach(System.out::println);

I want to write each line of my 2 maps in the same CSV file. I have the following code:

public void writeResultInCsv(Map<String, Long> resultMap) throws Exception {
    File csvOutputFile = new File(RUTA_FICHERO_RESULTADO);
    try (PrintWriter pw = new PrintWriter(csvOutputFile)) {
        resultMap.entrySet().stream()
                .map(this::convertToCSV)
                .forEach(pw::println);
    }
}

public String convertToCSV(String[] data) {
    return Stream.of(data)
            .map(this::escapeSpecialCharacters)
            .collect(Collectors.joining("="));
}

public String escapeSpecialCharacters(String data) {
    String escapedData = data.replaceAll("\\R", " ");
    if (data.contains(",") || data.contains("\"") || data.contains("'")) {
        data = data.replace("\"", "\"\"");
        escapedData = "\"" + data + "\"";
    }
    return escapedData;
}

But I get compilation error in writeResultInCsv method, in the following line: .map(this::convertToCSV)

This is the compilation error I get: reason: Incompatible types: Entry is not convertible to String[]

How can I indicate the following result in a CSV file in Java 8 in a simplified way? This is the result and design that I want my CSV file to have:

Countries:
England=24
Spain=21
Italy=10
etc

Cities:
London=10
Manchester=5
Madrid=7
Barcelona=4
Roma=3
etc

1 Answer 1

1

Your resultMap.entrySet() is a Set<Map.Entry<String, Long>>. You then turn that into a Stream<Map.Entry<String, Long>>, and then run .map on this. Thus, the mapper you provide there needs to map objects of type Map.Entry<String, Long> to whatever you like. but you pass the convertToCSV method to it, which maps string arrays.

Your code tries to join on comma (Collectors.joining(",")), but your desired output contains zero commas.

It feels like one of two things is going on:

  1. you copy/pasted this code from someplace or it was provided to you and you have no idea what any of it does. I would advise tearing this code into pieces: Take each individual piece, experiment with it until you understand it, then put it back together again and now you know what you're looking at. At that point you would know that having Collectors.joining(",") in this makes no sense whatsoever, and that you're trying to map an entry of String, Long using a mapping function that maps string arrays - which obviously doesn't work.

  2. You would know all this stuff but you haven't bothered to actually look at your code. That seems a bit surprising, so I don't think this is it. But if it is - the code you have is so unrelated to the job you want to do, that you might as well remove your code entirely and turn this question into: "I have this. I want this. How do I do it?"

NB: A text file listing key=value pairs is not usually called a CSV file.

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

2 Comments

I have already removed the compilation errors but I cannot get it to write to the CSV. CSV file is created correctly but does not write characters to me. Please, can you help me?
Did you call .close() on your printwriter after you are done?

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.