Что примерно так:
открытый класс CountryFunctionalTest {
private class CarMaker {
private final String name;
private final List<String> models;
public CarMaker(String name, List<String> models) {
this.name = name;
this.models = models;
}
public String getName() {
return name;
}
public List<String> getModels() {
return models;
}
}
private class Country {
private final String name;
private final List<CarMaker> makers;
public Country(String name, List<CarMaker> makers) {
this.name = name;
this.makers = makers;
}
public String getName() {
return name;
}
public List<CarMaker> getMakers() {
return makers;
}
}
@Test
public void test() throws DeliveryException {
List<Country> countries = Arrays.asList(new Country[]{
new Country("Country A", Arrays.asList(new CarMaker[]{ new CarMaker("Maker A", Arrays.asList(new String [] {"model a", "model a1", "model a2"})) })),
new Country("Country B", Arrays.asList(new CarMaker[]{ new CarMaker("Maker B", Arrays.asList(new String [] {"model b", "model b1"})) })),
new Country("Country C", Arrays.asList(new CarMaker[]{ new CarMaker("Maker C", Arrays.asList(new String [] {"model c", "model c1", "model c2", "model c3"})) }))
});
Map<Country, Integer> conuntriesModels = IntStream.range(0, countries.size()).mapToObj(i -> new AbstractMap.SimpleEntry<Country, Integer>(countries.get(i),
IntStream.range(0, countries.get(i).getMakers().size())
.map(ix -> countries.get(i).getMakers().get(ix).getModels().size()).sum()
)
).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
for (Country next : conuntriesModels.keySet()) {
System.out.println(next.getName() + " models -> " + conuntriesModels.get(next));
}
}
}
Вывод:
Модели страны B -> 2
Модели Country A -> 3
Модели Country C -> 4