简体   繁体   English

如何使用lambda或流从包含另一个对象列表的对象列表中迭代和获取对象

[英]How to iterate and get the object from list of object which contains another list of list of object using lambdas or stream

Assume the data collection object looks like below 假设数据收集对象如下所示

i need am trying to use java lambda function to get the matching brand object when the engine matches. 我需要尝试使用Java Lambda函数在引擎匹配时获取匹配的品牌对象。

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

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

now i need to get the brand name if the Engine type matched. 现在,如果引擎类型匹配,我需要获取品牌名称。 for example if i search Engine1 then the Honda object needs to be returned. 例如,如果我搜索Engine1,则需要返回本田对象。

below is the code sample: Class Vehicle: 下面是代码示例:Class 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;
    }
}

Class Brand: 类品牌:

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

Class Engine: 类引擎:

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

Class Main: 主班:

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

Above is the code i tried. 上面是我尝试过的代码。 earlier my objective was to filter with the key in the grant child object list. 早些时候,我的目标是使用授权子对象列表中的键进行过滤。

now i used that as a key to form a map with the parent or brand object as value. 现在,我将其用作键,以父项或品牌对象为值形成地图。 it is working for my data set. 它适用于我的数据集。 no null checks are done. 不执行空检查。

welcome suggestion on improvement. 欢迎提出改进建议。

You've left out a lot of details, so I'll assume you have a List<Brand> brands , and that Brand has a method List<Car> getCars() , and Car has a method List<Engine> getEngines() , and you've implemented Engine.equals() . 您已经省略了很多细节,所以我假设您有一个List<Brand> brands ,并且Brand拥有方法List<Car> getCars() ,而Car拥有方法List<Engine> getEngines() ,并且您已经实现了Engine.equals()

This method will return any brand with a matching engine, if one exists: 此方法将返回具有匹配引擎的任何品牌(如果存在):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用java流从包含多个不同mysql日期的列表对象中获取地图对象 - How to get a map object, from a list object which contains multiple different mysql dates using java stream 如何使用流从列表中的另一个对象检索列表 - How to retrieve a list from another object that is in the list using stream 使用java流从另一个对象列表更新对象列表 - Updating a List of Object from another list of object using java stream 如何使用 Java 8 Streams 迭代包含列表对象作为值的 Map 并将其作为单独的列表 Object - How to iterate over a Map that contains List Objects as value and get those as a separate List Object using Java 8 Streams 使用 Streams,查看列表是否包含来自另一个列表的 object 的属性 - Using Streams, see if a list contains a property of an object from another list 如何使用Java 8 Lambdas将对象列表转换为Map <Object,Object> - How to convert List of Objects into Map<Object, Object> using Java 8 Lambdas 如何使用Java 8 Lambda将对象添加到其他列表中的列表中 - How to add object into a list inside of the other list using Java 8 lambdas 如何在java 8中迭代对象数组列表并设置为另一个对象列表? - How to iterate List of object array and set to another object list in java 8? 如何使用 Stream API 从对象列表中获取 object 的值 - How to get Value of object from list of objects by Using Stream API 如何使用流过滤地图 <String, Object> 按清单 <String> 包含密钥 - How to filter, using stream, Map<String, Object> by List<String> which contains keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM