219

Is it possible to cast a stream in Java 8? Say I have a list of objects, I can do something like this to filter out all the additional objects:

Stream.of(objects).filter(c -> c instanceof Client)

After this though, if I want to do something with the clients I would need to cast each of them:

Stream.of(objects).filter(c -> c instanceof Client)
    .map(c -> ((Client) c).getID()).forEach(System.out::println);

This looks a little ugly. Is it possible to cast an entire stream to a different type? Like cast Stream<Object> to a Stream<Client>?

Please ignore the fact that doing things like this would probably mean bad design. We do stuff like this in my computer science class, so I was looking into the new features of java 8 and was curious if this was possible.

1
  • 3
    From the standpoint of the Java runtime the two Stream types are the same already, so no cast is required. The trick is to sneak it past the compiler. (That is, assuming it makes any sense to do so.)
    – Hot Licks
    Commented Mar 19, 2014 at 16:23

7 Answers 7

394

I don't think there is a way to do that out-of-the-box. A possibly cleaner solution would be:

Stream.of(objects)
    .filter(c -> c instanceof Client)
    .map(c -> (Client) c)
    .map(Client::getID)
    .forEach(System.out::println);

or, as suggested in the comments, you could use the cast method - the former may be easier to read though:

Stream.of(objects)
    .filter(Client.class::isInstance)
    .map(Client.class::cast)
    .map(Client::getID)
    .forEach(System.out::println);
6
  • This is pretty much what I was looking for. I guess I overlooked that casting it to Client in map would return a Stream<Client>. Thanks!
    – Phiction
    Commented Mar 19, 2014 at 16:35
  • +1 interesting new ways, although they risk to get in spaghetti-code of a new-generation type (horizontal, not vertical)
    – robermann
    Commented Mar 20, 2014 at 8:44
  • @LordOfThePigs Yes it works although I am not sure if the code gets clearer. I have added the idea to my answer.
    – assylias
    Commented Jun 22, 2014 at 22:38
  • 51
    You could "simplify" the instanceOf filter with: Stream.of(objects).filter(Client.class::isInstance).[...] Commented Nov 26, 2014 at 21:43
  • 1
    @T3rm1 when you work with raw types, many things become more difficult...
    – assylias
    Commented Feb 2, 2021 at 17:51
19

Along the lines of ggovan's answer, I do this as follows:

/**
 * Provides various high-order functions.
 */
public final class F {
    /**
     * When the returned {@code Function} is passed as an argument to
     * {@link Stream#flatMap}, the result is a stream of instances of
     * {@code cls}.
     */
    public static <E> Function<Object, Stream<E>> instancesOf(Class<E> cls) {
        return o -> cls.isInstance(o)
                ? Stream.of(cls.cast(o))
                : Stream.empty();
    }
}

Using this helper function:

Stream.of(objects).flatMap(F.instancesOf(Client.class))
        .map(Client::getId)
        .forEach(System.out::println);
13

Late to the party, but I think it is a useful answer.

flatMap would be the shortest way to do it.

Stream.of(objects).flatMap(o->(o instanceof Client)?Stream.of((Client)o):Stream.empty())

If o is a Client then create a Stream with a single element, otherwise use the empty stream. These streams will then be flattened into a Stream<Client>.

4
  • I tried to implement this, but got a warning saying that my class "uses unchecked or unsafe operations" – is that to be expected?
    – aweibell
    Commented Dec 10, 2014 at 12:57
  • unfortunately, yes. Were you to use an if/else rather than the ?: operator then there would be no warning. Rest assured you can safely supress the warning.
    – ggovan
    Commented Dec 10, 2014 at 17:46
  • 5
    Actually this is longer than Stream.of(objects).filter(o->o instanceof Client).map(o -> (Client)o) or even Stream.of(objects).filter(Client.class::isInstance).map(Client.class::cast).
    – Didier L
    Commented Jan 19, 2016 at 16:17
  • This gets cleaner with the pattern matching instanceof feature: Stream.of(objects).flatMap(o -> o instanceof Client c ? Stream.of(c) : Stream.empty())
    – M. Justin
    Commented Dec 22, 2023 at 0:31
8

This looks a little ugly. Is it possible to cast an entire stream to a different type? Like cast Stream<Object> to a Stream<Client>?

No that wouldn't be possible. This is not new in Java 8. This is specific to generics. A List<Object> is not a super type of List<String>, so you can't just cast a List<Object> to a List<String>.

Similar is the issue here. You can't cast Stream<Object> to Stream<Client>. Of course you can cast it indirectly like this:

Stream<Client> intStream = (Stream<Client>) (Stream<?>)stream;

but that is not safe, and might fail at runtime. The underlying reason for this is, generics in Java are implemented using erasure. So, there is no type information available about which type of Stream it is at runtime. Everything is just Stream.

BTW, what's wrong with your approach? Looks fine to me.

9
  • 2
    @D.R. Generics in C# is implemented using reification, while in Java, it is implemented using erasure. Both are implemented in different fashion underlying. So you can't expect it to work same way in both the languages.
    – Rohit Jain
    Commented Mar 19, 2014 at 16:18
  • 1
    @D.R. I understand that erasure poses many issues for the beginners to understand the concept of generics in Java. And since I don't use C#, I can't go into much detail about comparison. But the whole motivation behind implementing it this way IMO was to avoid major changes in JVM implementation.
    – Rohit Jain
    Commented Mar 19, 2014 at 16:22
  • 1
    Why would it "certainly fail at runtime"? As you say there's no (generic) type information, so nothing for the runtime to check. It might possibly fail at runtime, if the wrong types are fed through, but there is no "certainty" about that whatsoever.
    – Hot Licks
    Commented Mar 19, 2014 at 16:26
  • 2
    @RohitJain: I'm not criticizing Java's generic concept, but this single consequence still creates ugly code ;-)
    – D.R.
    Commented Mar 19, 2014 at 16:28
  • 1
    @D.R. - Java generics are ugly from the git-go. Mostly just C++ feature lust.
    – Hot Licks
    Commented Mar 19, 2014 at 16:28
2

The Stream.mapMulti method added in Java 16 can be used to cast and keep each element of the given type, skipping and excluding elements not of that type. In fact, using mapMulti in this way to filter and cast the stream is one of the examples shown in the method Javadocs:

Examples

Given a stream of Number objects, the following produces a list containing only the Integer objects:

     Stream<Number> numbers = ... ;
     List<Integer> integers = numbers.<Integer>mapMulti((number, consumer) -> {
             if (number instanceof Integer i)
                 consumer.accept(i);
         })
         .collect(Collectors.toList());

If you're doing a lot of such casting, you could create a resuable casting method that accepts the type to be cast to:

private static <T> BiConsumer<Object, Consumer<T>> casting(Class<T> clazz) {
    return (value, mapper) -> {
        if (clazz.isInstance(value)) {
            mapper.accept(clazz.cast(value));
        }
    };
}

// ...

List<Integer> integers = numbers.mapMulti(casting(Integer.class)).toList();
0

Here is a solution using a reusable Gatherer per the JEP 461: Stream Gatherers Java 22 preview language feature:

Gatherer<Object, Void, Integer> cast =
        Gatherer.of(Integrator.ofGreedy((state, element, downstream) -> {
    if (element instanceof Integer i) {
        return downstream.push(i);
    } else {
        return true;
    }
}));

// Returns [1, 2, 4, 5, 10]
List<Integer> integers = Stream.of(1, 2, 3.0, 4, 5, 6.0, true, 8L, "nine", 10)
        .gather(cast)
        .toList();

Or, the same thing but behind a generic method to cast to a requested type:

static <T> Gatherer<Object, ?, T> casting(Class<T> clazz) {
    return Gatherer.of(Integrator.ofGreedy((state, element, downstream) -> {
        if (clazz.isInstance(element)) {
            return downstream.push(clazz.cast(element));
        } else {
            return true;
        }
    }));
}

// ...

// Returns [1, 2, 4, 5, 10]
List<Integer> integers = Stream.of(1, 2, 3.0, 4, 5, 6.0, true, 8L, "nine", 10)
        .gather(casting(Integer.class))
        .toList();

This custom gatherer casts and keeps all elements of the given type, and skips any elements that aren't of that type.

Javadocs

Gatherer:

An intermediate operation that transforms a stream of input elements into a stream of output elements, optionally applying a final action when the end of the upstream is reached. […]

[…]

There are many examples of gathering operations, including but not limited to: grouping elements into batches (windowing functions); de-duplicating consecutively similar elements; incremental accumulation functions (prefix scan); incremental reordering functions, etc. The class Gatherers provides implementations of common gathering operations.

API Note:

A Gatherer is specified by four functions that work together to process input elements, optionally using intermediate state, and optionally perform a final action at the end of input. They are:

Stream.gather(Gatherer<? super T,?,R> gatherer):

Returns a stream consisting of the results of applying the given gatherer to the elements of this stream.

Gatherer.of(Gatherer.Integrator<Void,T,R> integrator)

Returns a new, parallelizable, and stateless Gatherer described by the given integrator.

3
  • I don’t think this brings much value over the mapMulti() approach, and the latter is probably even more efficient
    – Didier L
    Commented May 26 at 9:33
  • @DidierL I'm guessing it's a bit of a wash in this specific case. I think it's useful to be aware of as a tool in the toolbox. It also has the benefit of being composable with other gatherers, in case you have multiple gathering operations to perform at the same time, including a casting one.
    – M. Justin
    Commented May 27 at 21:26
  • I guess for chaining gatherers it makes sense, but then we would need gatherers for all existing intermediate operations, otherwise we are quite limited with out-of-the-box gatherers. It’s not clear for me whether they plan to do that.
    – Didier L
    Commented May 27 at 22:18
-2

I found when dealing with an untyped collection of one class you can create a typed collection with

UntypedObjCollection bag = some preGenerics method;
List<Foo> foolist = new ArrayList<>();
bag.stream().forEach(o ->fooList.add((Foo)o));

There doesn't seem to be a way to cast Object into something and filter, map, etc... in one shot. The only way to get Object into a typed collection is a terminal operation. This is running JDK 17 and dealing with module.base exceptions when trying less granular methods.

1
  • You can do it in a peek() too.
    – user207421
    Commented Dec 23, 2023 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.