当前位置: 首页 > 知识库问答 >
问题:

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

羊舌光赫
2023-03-14

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

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

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

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

现在,如果发动机类型匹配,我需要获得品牌名称。例如,如果搜索引擎1,则需要返回Honda对象。

下面是代码示例:类Vehicle:

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;
    }
}

类车:

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个答案

柯天宇
2023-03-14

您遗漏了很多细节,所以我假设您有一个<code>列表

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

Optional<Brand> findBrandByEngine(Engine engine) {
    return brands.stream()
            .filter(b -> b.getCars()
                    .stream()
                    .map(Car::getEngines)
                    .flatMap(List::stream)
                    .anyMatch(engine::equals))
            .findAny();
}
 类似资料: