-1

I'm still not good with functional programming and therefore need help. Could you please help me write the same thing using stream API.

Thanks in advance.

Map<Integer, Map<Integer, ArrayList<Integer>>> map = new TreeMap<>();
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
for(Map.Entry<Integer, Map<Integer, ArrayList<Integer>>> ele: map.entrySet()){
    ArrayList<Integer> temp = new ArrayList<>();
    for(Map.Entry<Integer, ArrayList<Integer>> ent: ele.getValue().entrySet()){
        temp.addAll(ent.getValue());
    }
    result.add(temp);
}

This is my approach. But there are some errors.

System.out.println(
    map.values()
        .stream()
        .map(ins -> { 
            ArrayList<Integer> temp = new ArrayList<>();
            ins.values().stream().forEach(temp::addAll);
            return temp;
        })
        .collect(Collectors.toList()));
double-beep
  • 5,031
  • 17
  • 33
  • 41
gcoderx
  • 53
  • 1
  • 6
  • The fact you are receiving a `cannot find symbol` leads me to believe that you have not correctly imported your packages. – shinjw Sep 02 '20 at 18:26
  • `import java.util.stream.Collectors;` – Yassin Hajaj Sep 02 '20 at 18:27
  • @gcoderx Have you tried my answer??? – Sagar Gangwal Sep 03 '20 at 04:54
  • I demand the reopening of the question. The question is not at all related to the question it has made to be associated with. It was a simple question of converting a nested hashmap of ArrayList to a line using stream API. Well, I have posted the solution in the question itself if somebody needs help with this. There were learning aspects of this but has been marked duplicate . I disagree but the decision is acceptable. – gcoderx Sep 03 '20 at 06:05
  • @YassinHajaj I have removed the error part. The question is all about conversion and not the imports. Even after the correct imports, it did not seem to work. Well, I have the solution ready now. So if you could please review it again, it'll be nice. – gcoderx Sep 03 '20 at 06:26
  • ArrayList> result = map.values().stream() .map(ins -> ins.values().stream() .flatMap(List::stream) .collect(Collectors.toCollection(ArrayList::new))) .collect(Collectors.toCollection(ArrayList::new)); return result; -- answer – gcoderx Sep 04 '20 at 20:09

1 Answers1

-1

You can try flatMap

Also instead of directly using temp::addAll, you need to try as below.

Update1

    System.out.println(
                    map.entrySet()
                            .stream()
                            .flatMap(ins -> ins.getValue().entrySet().stream()
                                    .map(integerListEntry -> integerListEntry.getValue())).collect(Collectors.toList()));

Update2

System.out.println(
                Arrays.asList(map.entrySet().stream()
                        .flatMap(ins -> ins.getValue().entrySet().stream()
                                .flatMap(integerListEntry->integerListEntry.getValue().stream())).collect(Collectors.toList())));

You can also create Method Reference as mentioned below. Here i had created method name with flatteringInnerMap and simply calling it by method reference.

   System.out.println(
            map.entrySet()
                    .stream()
                    .flatMap(ins -> ins.getValue().entrySet().stream()
                            .flatMap(Sample::flatteringInnerMap)).collect(Collectors.toList()));
}

private static Stream<? extends Integer> flatteringInnerMap(Map.Entry<Integer, List<Integer>> integerListEntry) {
    return integerListEntry.getValue().stream();
}

More about flatMap you will get idea here.

One more point i want to add here.

I recommend using "List" instead of "ArrayList" on the left side when creating list objects. It's better to pass around the interface "List" because then if later you need to change to using something like Vector (e.g. you now need synchronized lists), you only need to change the line with the "new" statement. No matter what implementation of list you use, e.g. Vector or ArrayList, you still always just pass around List.

This will help you.

Sagar Gangwal
  • 7,544
  • 3
  • 24
  • 38
  • 1
    Hi, I tried this. I wanted the result to be a nested ArrayList. But the output of this gives me a single list. Please look into this. Thank you very much for helping. And yeah, for the part that mentions that we generally use the interface in place of the class, I could not do much. This was required in the problem. But it was a good thing you told me about. – gcoderx Sep 03 '20 at 05:39
  • @gcoderx Try updated answer. – Sagar Gangwal Sep 03 '20 at 05:44
  • Hi, tried. This time it has not merged values of the inner hashmap. Please see the updated question where I have posted the solution. The post has been closed and so cannot answer. If possible, please simplify it a bit more. Thanks again for giving your time. – gcoderx Sep 03 '20 at 05:55
  • @gcoderx mine first answer is same only. You just need to create List from it. – Sagar Gangwal Sep 03 '20 at 06:21
  • Yeah, I see. Thanks – gcoderx Sep 03 '20 at 06:27
  • @gcoderx I had added updated answer. Instead of creating temporary list and adding all elements inside of it, you can implement using `flatMap` and collecting that `List as List` – Sagar Gangwal Sep 03 '20 at 06:32