【发布时间】:2016-03-09 23:19:09
【问题描述】:
我有一个Cars 列表,其中每辆车都有一个通过Engine 接口定义的引擎。在这个例子中,具体类型是CombustionEngine,具有可变数量的柱面,以及ElectricMotor。
我想找到所有四缸(燃烧)发动机。使用 Java 流我想出了这个管道:
Car[] carsWithFourCylinders
= cars.stream()
.filter( car -> car.engine instanceof CombustionEngine )
.filter( car -> ( ( CombustionEngine )car.engine ).cylinderCount == 4 )
.toArray( Car[]::new );
虽然这可行,但我想知道是否可以在第二个 filter 谓词中避免强制转换,或者完全重写管道以提高可读性?
为了参考和实验,我附上了示例的完整源代码:
public class CarTest {
interface Engine { }
class CombustionEngine implements Engine {
final int cylinderCount;
CombustionEngine( int cylinderCount ) {
this.cylinderCount = cylinderCount;
}
}
class ElectricMotor implements Engine { }
class Car {
final Engine engine;
Car( Engine engine ) {
this.engine = engine;
}
}
@Test
public void filterCarsWithFourCylinders() {
List<Car> cars = Arrays.asList( new Car( new CombustionEngine( 4 ) ),
new Car( new ElectricMotor() ),
new Car( new CombustionEngine( 6 ) ) );
Car[] carsWithFourCylinders
= cars.stream()
.filter( car -> car.engine instanceof CombustionEngine )
.filter( car -> ( ( CombustionEngine )car.engine ).cylinderCount == 4 )
.toArray( Car[]::new );
assertEquals( 1, carsWithFourCylinders.length );
}
}
【问题讨论】:
标签: java-stream