迭代java 8流中的对象

时间:2017-10-02 07:19:33

标签: mongodb lambda java-8 spring-data java-stream

我想在java8中迭代以下mongo db集合

{ "_id" : ObjectId("59d1dc99a1a79605342f15a7"), "_class" : "com.test.User", "vehicle" : [ { "name" : "aaa", "color" : "blue" } ], "count" : 1,"createdBy" : "Admin", "lastModifiedBy" : "Admin" } 

预期的O / P字段:

_id,名称,颜色,计数,createdBy

我尝试了下面的

queryvalue.stream().flatMap(vehicle->vehicle.getVehicles().stream()).collect(Collectors.toList());

queryvalue是我从spring mongo db repository中提取数据的那个。我是java8的新用户。

以下是课程:

Class User{
@CreatedDate
protected DateTime createdDate;
@LastModifiedDate
protected DateTime lastModifiedDate;
List<Vehicle> vehicle;
public void setCreatedDate(DateTime createdDate) {
        this.createdDate = createdDate;
    }
public String getCreatedDate() {
        return createdDate;
    }   
public List<Vehicle> getVehicles() {
        return vehicle;
    }
public void setVehicle(List<Vehicle> vehicle) {
        this.vehicle= vehicle;
    }
-- similarly for lastModifieddate and comments etc--    
}

Class Vehicle{
private String name;
private string color;
public void setColor(String color){
this.color = color;
return this;
}
public String getColor(){
return color;
}
public void setName(String name){
this.name = name;
return this;
}
public void getName(){
return name;
}
}

我正在使用它:

List<Vehicle>queryvalue  = repository.findByVehicleName(vehiclename);

0 个答案:

没有答案