partnersName =["partner1","partner2"];
totalDates = ["2022-05-18","2022-05-19"];
both the lists store string only
here is the following code which gives output that I want
for (int i = 0; i < partnersName.size(); i ) {
if (totalDates.isEmpty()) {
totalDates.add(partnersName.get(i));
} else {
for (int j = 0; j < totalDates.size(); j ) {
totalDates.set(j, partnersName.get(i) "/" totalDates.get(j));
}
}
}
output =["partner1/2022-05-18","partner2/2022-05-18","partner1/2022-05-19","partner2/2022-05-19"];
but I want to reduce the line of code using stream. Anyone could help to get stream API code
CodePudding user response:
Assuming the result [partner1/2022-05-18, partner1/2022-05-19, partner2/2022-05-18, partner2/2022-05-19]
is what you want:
List<String> result = partnersName.stream().<String>mapMulti((s, stringConsumer) -> totalDates.forEach(d -> stringConsumer.accept(s "/" d))).toList();
CodePudding user response:
Another way can be
List<String> result = partnersName.stream()
.flatMap(x -> totalDates.stream().map(y -> x.concat("/" y)))
.collect(Collectors.toList());
//output: [partner1/2022-05-18, partner1/2022-05-19, partner2/2022-05-18, partner2/2022-05-19]