我是java8的新手 我有以下课程。
class User {
List<Vehicle> vehicle;
private int vehiclecount;
public List<Vehicle> getVehicle() {
return vehicle;
}
}
class Vehicle{
String vehiclename;
String vehiclecolor;
}
我可以将它保存在mongo集合中:
{ "_id" : ObjectId("59ca1e53a1a79607fcc9200f"), "_class" : "com.test.User",
"vechicle" : [ { "vehiclename" : "Car", "vehiclecolor" : "Blue" } ],
"count" : 1, "createdDate" : ISODate("2017-09-26T09:30:59.826Z") }
现在我根据spring mongo数据存储库提取结果。我想迭代上面的mongo集合,这样我才能获得车辆清单 我尝试了下面的内容:
List<Vehicle> vehicle = result.stream().filter(vehicles->vehicles.getVehicle().stream().collect(Collectors.toList());
请帮助我刚接触java8。提前致谢
答案 0 :(得分:4)
如果我理解正确:
List<Vehicle> vehicles =
result.stream()
.flatMap(user -> user.getVehicle().stream())
.collect(Collectors.toList());