Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        2年前关闭。
                                                                                            
                
        
我是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的即时消息。提前致谢

最佳答案

如果我正确理解:

List<Vehicle> vehicles =
    result.stream()
       .flatMap(user -> user.getVehicle().stream())
       .collect(Collectors.toList());

10-07 20:50