Relative Content

Tag Archive for collections

Is there an alternative to instanceof when filtering a Java stream by class?

I have an unexpected situation in a project in which all types extending one class are packed into a Java collection; but only a specific extension of that class contains an additional method. Let’s call it “also()”; and let me be clear that no other extension has it. Right before performing one task on every item in that collection, I need to call also() on every item that implements it.