I have a function with an Object
as parameter. This object may be a collection in the most generic sense possible: it can be a list, a map, iterable, etc., in which case I want to process each item belonging to it:
public void f(Object o) {
if (o instanceof SOMECLASSORINTERFACE<?>) {
Stream.of(o).map( .. )...;
} else {
// o is scalar
...
}
}
The code above doesn't work: Stream.of()
doesn't split my object into its elements to stream, but only outputs one element, the object o
itself.
I cannot use o.stream().map...
because o
is too generic and may not have the stream
method.
Casting o
to Collection
does not work. Also, checking for Collection
membership is probably not the right thing to do...
So how do I obtain a stream out of a generic collection?