如何使用lambdas或stream从包含另一个对象列表列表的对象列表中迭代并获取对象

时间:2017-03-23 23:33:22

标签: java lambda java-stream

假设数据收集对象如下所示

我需要尝试使用java lambda函数在引擎匹配时获取匹配的品牌对象。

Vehicles Class
   - list of Brand
            - List of cars
                    - List of engine

Vehicles
   - Honda
         - Civic
              - Engine1
              - Engine2
         - Accord 
              - Engine11
              - Engine12
   - Toyota 
         - corolla 
              - Engine21

现在我需要获得品牌名称,如果引擎类型匹配。 例如,如果我搜索Engine1,则需要返回本田对象。

下面是代码示例: 类车辆:

import java.util.List;
public class Vehicle {
    private List<Brands> brands;
    public List<Brands> getBrands() {
        return brands;
    }
    public void setBrands(List<Brands> brands) {
        this.brands = brands;
    }
}

班级品牌:

import java.util.List;
public class Brands {
    private String brandname;
    private List<Cars> cars;
    public String getBrandname() {
        return brandname;
    }
    public void setBrandname(String brandname) {
        this.brandname = brandname;
    }
    public List<Cars> getCars() {
        return cars;
    }
    public void setCars(List<Cars> cars) {
        this.cars = cars;
    }
}

Class Cars:

import java.util.List;
public class Cars {
    private List<Engines> engines;
    public List<Engines> getEngines() {
        return engines;
    }
    public void setEngines(List<Engines> engines) {
        this.engines = engines;
    }
}

班级引擎:

import java.util.List;
public class Engines {
    private List<String> names;
    public List<String> getNames() {
        return names;
    }
    public void setNames(List<String> names) {
        this.names = names;
    }
}

班级主要:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StreamDemoMain {
    public static void main(String args[]){


        Vehicle vh = new Vehicle();

        List<Brands> brandslist =  new ArrayList<>();
        Brands brand = new Brands();
        Cars car = new Cars();
        Engines engine = new Engines();
        List<Engines> engineList = new ArrayList<>();
        List<String> engineNames = new ArrayList<>();
        List<Cars> carList = new ArrayList<>();
        brand.setBrandname("toyota");

        engineNames.add(brand.getBrandname()+"1");
        engineNames.add(brand.getBrandname()+"2");
        engine.setNames(engineNames);
        engineList.add(engine);
        car.setEngines(engineList);
        carList.add(car);
        brand.setCars(carList);
        brandslist.add(brand);

         brand = new Brands();
         car = new Cars();
         engine = new Engines();
         engineList = new ArrayList<>();
         engineNames = new ArrayList<>();
        carList = new ArrayList<>();
        brand.setBrandname("honda");

        engineNames.add(brand.getBrandname()+"1");
        engineNames.add(brand.getBrandname()+"2");
        engine.setNames(engineNames);
        engineList.add(engine);
        car.setEngines(engineList);
        carList.add(car);
        brand.setCars(carList);
        brandslist.add(brand);
        vh.setBrands(brandslist);

        Map<String,Brands> result = new HashMap<>();
             vh.getBrands().stream()
            .forEach(eachcars->{eachcars.getCars().stream()
                                .map(Cars::getEngines)
                                .flatMap(List::stream)
                                .peek(a->{System.out.println("before:: "+a.getNames().toString());})
                                .map(Engines::getNames)
                                .flatMap(List::stream)
                                .forEach(engineName -> {
                                    result.put(engineName, eachcars);
                                });
                }
                );

             //print results and validate the output object
             List<Brands> listofBrands = new ArrayList<>();
             result.entrySet().stream().forEach(values->{
                 System.out.println(values.getKey() + "  = "+ values.getValue().getBrandname());
                 if(!listofBrands.contains(values.getValue())){
                     listofBrands.add(values.getValue());
                 }
             });

             System.out.println(listofBrands.size());
    }
}

以上是我试过的代码。 早些时候,我的目标是使用授权子对象列表中的密钥进行过滤。

现在我用它作为关键来形成父母或品牌对象作为值的地图。 它适用于我的数据集。没有进行空检查。

欢迎提出改进建议。

1 个答案:

答案 0 :(得分:3)

您已经遗漏了很多细节,因此我假设您有一个List<Brand> brands,并且Brand有一个方法List<Car> getCars()和{{ 1}}有一个方法Car,您已经实施了List<Engine> getEngines()

此方法将返回具有匹配引擎的任何品牌(如果存在):

Engine.equals()