Source:
<code>public void updateCertifiedProductMeasureMapping(ListingMeasure toUpdate) throws EntityRetrievalException { ListingMeasureEntity existingEntity = getEntityById(toUpdate.getId()); if (existingEntity == null) { throw new EntityRetrievalException("Could not find mapping with id " + toUpdate.getId()); } existingEntity.setMeasureId(toUpdate.getMeasure().getId()); existingEntity.setTypeId(toUpdate.getMeasurementType().getId()); existingEntity.setLastModifiedUser(AuthUtil.getAuditId()); update(existingEntity); Optional<List<CertificationCriterion>> updatedAssociatedCriteria = Optional .ofNullable(new ArrayList(toUpdate.getAssociatedCriteria())); Optional<List<CertificationCriterion>> exsitingAssociatedCriteria = Optional .ofNullable(existingEntity.getAssociatedCriteria().stream() .map(assocCriterion -> assocCriterion.convert()) .collect(Collectors.toList())); List<CertificationCriterion> removed = getRemovedAssociatedCriteria(updatedAssociatedCriteria, exsitingAssociatedCriteria); List<CertificationCriterion> added = getAddedAssociatedCriteria(updatedAssociatedCriteria, exsitingAssociatedCriteria); <START> removed.stream().forEach(removedCriterion -> { Optional<ListingMeasureCriterionMapEntity> removedEntityOpt = existingEntity.getAssociatedCriteria().stream() .filter(existingCrit -> existingCrit.getCriterion().getId().equals(removedCriterion.getId())) .findAny(); if (removedEntityOpt.isPresent()) { ListingMeasureCriterionMapEntity removedEntity = removedEntityOpt.get(); removedEntity.setDeleted(true); removedEntity.setLastModifiedUser(AuthUtil.getAuditId()); update(removedEntity); } }); <END> added.stream().forEach(addedCriterion -> { ListingMeasureCriterionMapEntity addedEntity = new ListingMeasureCriterionMapEntity(); addedEntity.setCertificationCriterionId(addedCriterion.getId()); addedEntity.setDeleted(false); addedEntity.setLastModifiedUser(AuthUtil.getAuditId()); addedEntity.setListingMeasureMapId(existingEntity.getId()); create(addedEntity); }); }</code><technical_language>For readability, want pull code in foreach a new method is named based is happening. improve readability. removed.stream() .foreach(removedCriteria -> deleteCriteria(removedCriteria));</technical_language>

Target:
public void updateCertifiedProductMeasureMapping(ListingMeasure toUpdate) throws EntityRetrievalException { ListingMeasureEntity existingEntity = getEntityById(toUpdate.getId()); if (existingEntity == null) { throw new EntityRetrievalException("Could not find mapping with id " + toUpdate.getId()); } existingEntity.setMeasureId(toUpdate.getMeasure().getId()); existingEntity.setTypeId(toUpdate.getMeasureType().getId()); existingEntity.setLastModifiedUser(AuthUtil.getAuditId()); update(existingEntity); Optional<List<CertificationCriterion>> updatedAssociatedCriteria = Optional .ofNullable(new ArrayList(toUpdate.getAssociatedCriteria())); Optional<List<CertificationCriterion>> exsitingAssociatedCriteria = Optional .ofNullable(existingEntity.getAssociatedCriteria().stream() .map(assocCriterion -> assocCriterion.convert()) .collect(Collectors.toList())); List<CertificationCriterion> removed = getRemovedAssociatedCriteria(updatedAssociatedCriteria, exsitingAssociatedCriteria); List<CertificationCriterion> added = getAddedAssociatedCriteria(updatedAssociatedCriteria, exsitingAssociatedCriteria); removed.stream().forEach(removedCriterion -> deleteRemovedCriterion(existingEntity, removedCriterion)); added.stream().forEach(addedCriterion -> saveAddedCriterion(existingEntity, addedCriterion)); }