0

I have the following code:

for (final String productId : entry.getValue()) {
    final Product pro = catalogAwareRepository.findUniqueByCatalogAndVersion(Constants.Id, productId, catalog, version, Product.class);
    if (pro != null) {
      productDTOs.add(productConverter.itemDTOfFromItem(product));
    }
}

I converted it to use Java 8 streams like this:

entry.getValue()
    .parallelStream()
    .map(productId -> catalogAwareRepository.findUniqueByCatalogAndVersion(Constants.Id, productId, catalog, version, Product.class))
    .forEach(pro -> productDTOs.add(productConverter.itemDTOfFromItem(pro)));

Is this the correct translation? Also, how do you perform a null check inside parallelStream operation?

Translation after Tunaki suggestion

entry.getValue().parallelStream().map(productId -> catalogAwareRepository.findUniqueByCatalogAndVersion(Constants.Id, productId, catalog, version, Product.class))
              .filter(Objects::nonNull).map(pro -> productDTOs.add(productConverter.itemDTOfFromItem(pro))).collect(Collectors.toList());
Saurabh Kumar
  • 16,353
  • 49
  • 133
  • 212

0 Answers0