[英]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.