In my Java 11 application, I want to get Product Updates from a repository. One Product Update has a updateId
and a list of productIds
to update.
If there are no product numbers that should be updated for update with
updateId = X
, I still want to write to another table that I've processed the updateX
;updateStatusRepository.setStatusProcessing(updateId)
andupdateStatusRepository.setStatusProcessed(updateId)
should still be called for thisupdateId
.If there are product updates present, they should be processed in the
ProductProcessingService
.
For now, the groupingBy
and mapping
give me a Set with a null
entry instead of an empty set, which is why I later remove all null
Product IDs.
List<ProductUpdate> productUpdateList = updateStatusRepository.getProductUpdates();
Map<String, Set<String>> productUpdateMap = productUpdateList
.stream()
.collect(
Collectors.groupingBy(
ProductUpdate::getUpdateId,
Collectors.mapping(ProductUpdate::getProductNo, Collectors.toSet())));
productUpdateMap.forEach(
(updateId, productIds) -> {
try {
updateStatusRepository.setStatusProcessing(updateId);
productIds.remove(null);
if(!productIds.isEmpty()) {
productProcessingService.performProcessing(Lists.newArrayList(productIds));
}
updateStatusRepository.setStatusProcessed(updateId);
} catch (Exception e) {
//
}
});
I'd prefer if it were possible to use mapping
in such a way that it delivers an empty Set directly if all values are null
.
Is there a way to do this elegantly?