简体   繁体   中英

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.

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.

below is the code sample: 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() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM