0

I have the following code. And the maps structure is 'Map<String, Long[]> Map' and it has the following values:

<com.service.a, 21221, 2121>

<com.service.b, 5454, 8787>

<com.service.c, 1227, 0>

public static void savetoFile(Map<String, Long[]> result, String filepath) throws IOException 
    {
        FileWriter fw = new FileWriter(filepath,true);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter pw = new PrintWriter(bw);
        for (String name: result.keySet())
            {
                String key =name.toString();
                Long[] value = result.get(name);  
                //System.out.println(key + " " + value);

                pw.println(key+","+value);       
            } 
        pw.flush();
        pw.close();
    }

On running this function I get the following output:

{com.service.a=[Ljava.lang.Long;@135fbaa4, com.service.b=[Ljava.lang.Long;@4b67cf4d, com.service.c=[Ljava.lang.Long;@7ea987ac,}

How to get rid of this error? I have gone through certain link where they suggest to use toString() method but I'm not sure how to apply that within this function.

I want the above values to be stored into CSV file as :

com.service.a, 21221, 2121

com.service.b, 5454, 8787

com.service.c, 1227, 0

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
nt fury
  • 39
  • 1
  • 9
  • 4
    There's no error here - that's the result of calling `toString()` on an array. If you want to print out the values, use `Arrays.toString(value)`. (This has nothing to do with the value that the array is in a map, either. You'd see the same kind of output if you just wrote: `Long[] array = { 1L, 2L }; System.out.println(array);` – Jon Skeet Jul 14 '17 at 09:36
  • @JonSkeet actually I'm writing it to a CSV file and in the CSV file it is saving it as com.service.a=[Ljava.lang.Long;@135fbaa4, com.service.b=[Ljava.lang.Long;@4b67cf4d, com.service.c=[Ljava.lang.Long;@7ea987ac, – nt fury Jul 14 '17 at 09:38
  • 4
    I don't see how that relates to my comment at all. I've explained why you're getting that output and how to fix it. – Jon Skeet Jul 14 '17 at 09:41
  • I betting that if you write into your post what you want each entry's line to look like, then you will probably discover what is wrong with your code. – Jeff Holt Jul 14 '17 at 09:42
  • you may change pw.println(key+","+value); to pw.println(key+","+value[0]+","+value[1]); – Eritrean Jul 14 '17 at 09:42
  • @Eritrean that assumes two values in each array. Shouldn't make that assumption. – Michael Markidis Jul 14 '17 at 09:45
  • @Eritrean thanks!!!!!!!!!!!!! – nt fury Jul 14 '17 at 09:55
  • @JonSkeet thank you......................... – nt fury Jul 14 '17 at 09:56
  • @Eritrean, Java 8 has `String.join(",", Arrays.asList(array))`. Some libraries have that even in Java 1.1. – M. Prokhorov Jul 14 '17 at 10:01

2 Answers2

1

When you call pw.println(key+","+value); you call .toString() on Long[] - the default toString method returns name of the class of the object + "@" + hashcode of the object. If you want human-readable value you need to call Arrays.toString(value), instead of value.

Gaskin
  • 11
  • 3
1

Consider building the string for each record as follows:

for (String name: result.keySet())
{
    String key =name.toString();
    Long[] value = result.get(name);  
    //System.out.println(key + " " + value);

    StringBuilder record = new StringBuilder();

    record.append(key);

    for (long l : value)
    {
        record.append(", ");
        record.append(l);
    }

    pw.println(record.toString());       
}
Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
  • Your result will have a loose comma in the beginning. You have to compensate for that. Or in Java 8 use `StringJoiner` class or simply `String.join(", ", Arrays.asList(values))`. – M. Prokhorov Jul 14 '17 at 10:04
  • @M.Prokhorov What do you mean, "Your result will have a loose comma in the beginning."? Are you considering the entire csv record (i.e. the key as well)? – Michael Markidis Jul 14 '17 at 10:07
  • @MichaelMarkidis, I'm considering value without a key. I suppose with a key added in it will be a valid csv, but this answer is very question-specific like that. – M. Prokhorov Jul 14 '17 at 10:19