[英]Java 8: extract a substream from a stream
I have a stream of Objects, from which I need to extract a Stream which has only some of the object attributes. 我有一个对象流,我需要从中提取一个只有一些对象属性的Stream。
For example, from a Stream<Car>
, I need to extract a Stream<CarDetails>
. 例如,从
Stream<Car>
,我需要提取Stream<CarDetails>
。
Car {
String name;
String model;
Engine e;
CarType t;
...
}
I want to extract a Stream
of objects having type CarDetails
: 我想提取一个类型为
CarDetails
的对象Stream
:
CarDetails {
String name;
String model;
}
You can achieve this with map
, assuming you have the required CarDetails
constructor : 假设您具有所需的
CarDetails
构造函数,则可以使用map
实现此CarDetails
:
Stream<Car> cars = ...
Stream<CarDetails> details = cars.map(c -> new CarDetails(c.getName(),c.getModel()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.