Как можно преобразовать следующий код с вложенным циклом foreach с использованием Stream API?
private static final int calculateMaximumSeedsInOneBox() {
int maximum = 0;
int currentGranateSeeds = 0;
int currentBoxSeeds = 0;
for (Box box : boxesWithPomegrantes) {
for (Pomegranate pomegranate : box) {
currentGranateSeeds = pomegranate.getSeeds().size();
}
currentBoxSeeds += currentGranateSeeds;
if (currentBoxSeeds > maximum) {
maximum = currentBoxSeeds;
}
}
return maximum;
}
private static List<String> findNamesOfBoxesWithMaximumSeeds() {
List<String> namesOfBoxes = new ArrayList<>();
int maximum = calculateMaximumSeedsInOneBox();
int currentGranateSeeds = 0;
int currentBoxSeeds = 0;
for (Box box : boxesWithPomegrantes) {
for (Pomegranate pomegranate : box) {
currentGranateSeeds = pomegranate.getSeeds().size();
}
currentBoxSeeds += currentGranateSeeds;
if (currentBoxSeeds == maximum) {
namesOfBoxes.add(box.getName());
}
}
return namesOfBoxes;
}