Java 8: extracting sub streams from streams
•
Java
I have an object stream from which I need to extract a stream with only some object properties
For example, from stream < car >, I need to extract stream < carddetails >
Car { String name; String model; Engine e; CarType t; ... }
I want to extract an object stream of type cardetails:
CarDetails { String name; String model; }
Solution
Assuming you have the required cardetails constructor, you can do this using map:
Stream<Car> cars = ... Stream<CarDetails> details = cars.map(c -> new CarDetails(c.getName(),c.getModel()));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码