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
  • 57
  • 11
  • 26

2 Answers2

3

If you want to get one ArrayList then use

ArrayList<Object> result = strm.flatMap(ArrayList::stream)
        .collect(Collectors.toCollection(ArrayList::new));
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
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
  • 546
  • 4
  • 11