0

I have a Stream<ArrayList<Object>> and I want to "extract" the ArrayList from it and assign it to a variable. How do I do that?

My resulting variable needs to be of type ArrayList<Object> so I can iterate over it and do stuff.

Ivan
  • 397
  • 6
  • 22

2 Answers2

3

If you want to get one ArrayList then use

ArrayList<Object> result = strm.flatMap(ArrayList::stream)
        .collect(Collectors.toCollection(ArrayList::new));
YCF_L
  • 51,266
  • 13
  • 85
  • 129
1

Stream.flatMap method lets you replace each value of a stream with another stream and then concatenates all the generated streams into a single stream.

List<Object> objectList = new ArrayList<>();
        List<Object> collect = Stream.of(objectList)
                .flatMap(m -> m.stream())
                .collect(Collectors.toList());
Michael Mesfin
  • 386
  • 2
  • 10