0

I am reading a CSV file and want to group records using 17 th column. I want all the rows corresponding to a key as a list/Array but i am getting somthing Else. Please help

void   read_CSV_file(String path )
    {
        Stream<String > file_data_by_lines = null;

        try {
            file_data_by_lines = Files.lines(Paths.get(path)) ;
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        Map temp =file_data_by_lines.map(x-> (x.split(",")))
                                            .collect( Collectors.groupingBy(x-> x[17], Collectors.toSet()));



        temp.values().stream().forEach(System.out::println);


    }

i am geting output as below :

[[Ljava.lang.String;@7a46a697, [Ljava.lang.String;@6d86b085, [Ljava.lang.String;@68f7aae2, [Ljava.lang.String;@4141d797, [Ljava.lang.String;@5f205aa, [Ljava.lang.String;@7530d0a, [Ljava.lang.String;@2a3046da, [Ljava.lang.String;@75828a0f, [Ljava.lang.String;@2f410acf] [[Ljava.lang.String;@2401f4c3, [Ljava.lang.String;@3b6eb2ec, [Ljava.lang.String;@2d363fb3, [Ljava.lang.String;@21bcffb5, [Ljava.lang.String;@566776ad, [Ljava.lang.String;@34c45dca, [[Ljava.lang.String;@1d251891, [Ljava.lang.String;@1edf1c96] [[Ljava.lang.String;@28f67ac7, [Ljava.lang.String;@129a8472]

Problem Solved by using following code ,

temp.values().stream().forEach(x-> System.out.println(Arrays.deepToString(x.toArray())));

  • 1. Don't use raw type `Map temp` and 2. you might want to be`mapping` the values before collecting `toSet` and then you would need to flatten the values `temp.values().stream().flatMap(Set::stream).forEach..` – Naman Jun 29 '20 at 13:15
  • You are printing set of String array, use `Map> temp` and print as @Naman suggested – Eklavya Jun 29 '20 at 13:23
  • You should look at this for fixing the output: [What's the simplest way to print a Java array?](https://stackoverflow.com/q/409784/5221149) – Andreas Jun 29 '20 at 13:32
  • 1
    The map values are type `Set`, which makes no sense at all, since arrays don't implement `equals()` and `hashCode()`, so it might as well be `List` – Andreas Jun 29 '20 at 13:36
  • @Andreas it worked man .. Thanks a lot . I cahnged the code to temp.values().stream().forEach(x-> System.out.println(Arrays.deepToString(x.toArray()))); – Nil Kulkarni Jun 29 '20 at 14:03

0 Answers0