Casting a list during a stream operation

Viewed 106

Here's my scenario:

private List<Entity> getPlanets() {
   return entities.values()
                  .stream()
                  .filter(x -> x instanceof Planet)
                  .collect(Collectors.toList());
}
  • Entity is the super class of Planet
  • entities is a HashMap<Entity>
  • As the method is called "getPlanets" I would like it to return a List<Planet> but it appears to me that the stream expression is going to return a List<Entity>
  • I tried some casting expressions but none seem to work out.

I am brand new with Java 8 streams so maybe someone can point out what I am missing?

2 Answers
Related