18

I have a list of objects, some of them can be collections. I would like to get a stream of plain objects.

List<Object> objects = List.of(1, 2, "SomeString", List.of(3, 4, 5, 6), 
    7, List.of("a", "b", "c"),
    List.of(8, List.of(9, List.of(10))));

I would like to get a stream with elements.

1, 2, "SomeString", 3, 4, 5, 6, 7, "a", "b", "c", 8, 9, 10

I have tried

Function<Object, Stream<Object>> mbjectToStreamMapper = null; //define it. I have not figured it out yet!
objects.stream().flatMap(ObjectToStreamMapper).forEach(System.out::println);

I also checked an example which shows how to use a recursive function which flattens a collection. However, in this example .collect(Collectors.toList()); used to keep an intermediate result. Collectors.toList() is a terminal operation, which will start processing the stream right away. I would like to get a stream, which I can iterate on later.


Update

I agree with comments, it is a terrible idea to have a stream composed of objects of different nature. I just wrote this question for simplicity. In real life, it can be that I listen to different events, and process some business objects from incoming streams, some of them can send stream of objects, other - just single objects.

Yan Khonski
  • 12,225
  • 15
  • 76
  • 114
  • 4
    "I have a list of objects, some of them can be collections." Unless your actual objects have a more useful common supertype, there is very little you can actually use this list for - irrespective of the streams - without some kind of reflection. So the solution (nearly) unavoidably involves reflection. – Andy Turner Feb 03 '19 at 16:30
  • 1
    are you sure, you need to store all of these objects in the same list? Sounds like a horrible idea to me – László Stahorszki Feb 03 '19 at 16:40
  • @LászlóStahorszki, Yes, I agree with you. Just for simplicity sake. For example, I need to process events, updatedEvents, complexEvents, and they come from different sources. As I wrote in update. – Yan Khonski Feb 03 '19 at 16:47
  • 1
    I'm quite sure you can create a common interface, which all of the list members can implement – László Stahorszki Feb 03 '19 at 21:21
  • 1
    Don't put such a mixture anywhere. Instead wrap the things in an object implementing a common interface containing the streaming method. Then you have `List` which is much nicer and can be processed with `objects.stream().flatMap(MyStreamableThing::toStream)`. – maaartinus Feb 04 '19 at 05:28

4 Answers4

16
class Loop {
    private static Stream<Object> flat(Object o) {
        return o instanceof Collection ?
                ((Collection) o).stream().flatMap(Loop::flat) : Stream.of(o);
    }

    public static void main(String[] args) {
        List<Object> objects = List.of(1, 2, "SomeString", List.of( 3, 4, 5, 6),
                7, List.of("a", "b", "c"), List.of(8, List.of(9, List.of(10))));

        List<Object> flat = flat(objects).collect(Collectors.toList());

        System.out.println(flat);
    }
}

Please note List.of(null) throws NPE.

Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
6

We can recursively get the nested stream if the object being traversed is an instance of Collection.

public static void main(String args[]) {
       List<Object> objects = List.of(1, 2, "SomeString", List.of(3, 4, 5, 6),
            7, List.of("a", "b", "c"),
            List.of(8, List.of(9, List.of(10))));
       List<Object> list = objects.stream().flatMap(c -> getNestedStream(c)).collect(Collectors.toList());
}

public static Stream<Object> getNestedStream(Object obj) {
    if(obj instanceof Collection){
        return ((Collection)obj).stream().flatMap((coll) -> getNestedStream(coll));
    }
    return Stream.of(obj);
}
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
2

Note, it's possible to define recursive methods in a field:

public class Test {
  static Function<Object,Stream<?>> flat=
    s->s instanceof Collection ? ((Collection<?>)s).stream().flatMap(Test.flat) : Stream.of(s);
  public static void main(String[] args) {
    objects.stream().flatMap(flat).forEach(System.out::print);
  }
}
Marc Dzaebel
  • 425
  • 4
  • 12
0

I improved the code of accepted answer and it's working fine.

    <E> Stream<E> getNestedStream(E input, Function<E, List<E>> mapper) {
        if(Objects.isNull(input)) {
            return Stream.empty();
        }
        Collection<E> values = mapper.apply(input);
        if(Objects.isNull(values)) {
            return Stream.of(input);
        }
        return Stream.concat(Stream.of(input), mapper.apply(input).stream().flatMap(obj -> getNestedStream(obj, mapper)));
    }
Abhishek Kumar
  • 197
  • 2
  • 15