Let’s say I have this simple class structure:
Vehicle
|-- Bus
`-- Car
Bus and Car extends Vehicle
I have List<Vehicle>. How can I get only buses or cars? I would like to avoid instanceof operator. Do I have to use Visitor pattern or is there any simpler solution?
Well something will need to use
instanceofor an equivalent (such asClass.isInstance).Guava has a method which does exactly this though:
Iterables.filter. So you could use: