Source:
public void validateCodes(final BindingResult errors, final List<GermplasmUpdateDTO> germplasmUpdateDTOList) { final Set<String> nameCodes = new HashSet<>(); germplasmUpdateDTOList .forEach(g -> nameCodes.addAll(g.getNames().keySet().stream().map(n -> n.toUpperCase()).collect(Collectors.toList()))); nameCodes.addAll( germplasmUpdateDTOList.stream().map(o -> StringUtils.isNotEmpty(o.getPreferredName()) ? o.getPreferredName() : null) .filter(Objects::nonNull).collect( Collectors.toSet())); final List<String> existingNamesCode = this.germplasmService.filterGermplasmNameTypes(nameCodes).stream().map(GermplasmNameTypeDTO::getCode).collect( Collectors.toList()); final Set<String> attributesCode = new HashSet<>(); germplasmUpdateDTOList.stream().filter(germ -> germ.getAttributes() != null).collect(Collectors.toList()) .forEach( g -> attributesCode.addAll(g.getAttributes().keySet().stream().map(n -> n.toUpperCase()).collect(Collectors.toList()))); final List<String> existingAttributesCode = this.germplasmService.filterGermplasmAttributes(attributesCode).stream().map(AttributeDTO::getCode).collect( Collectors.toList()); nameCodes.removeAll(existingNamesCode); attributesCode.removeAll(existingAttributesCode); if (!nameCodes.isEmpty()) { errors.reject("germplasm.update.invalid.name.code", new String[] {String.join(",", nameCodes)}, ""); } if (!attributesCode.isEmpty()) { errors.reject("germplasm.update.invalid.attribute.code", new String[] {String.join(",", attributesCode)}, ""); } final List<String> ambiguosCodes = new ArrayList<>(CollectionUtils.intersection(existingNamesCode, existingAttributesCode)); if (!ambiguosCodes.isEmpty()) { errors.reject("germplasm.update.ambiguous.code", new String[] {String.join(",", ambiguosCodes)}, ""); } }

Target:
public void validateCodes(final BindingResult errors, final List<GermplasmUpdateDTO> germplasmUpdateDTOList) { final Set<String> nameCodes = new HashSet<>(); germplasmUpdateDTOList .forEach(g -> nameCodes.addAll(g.getNames().keySet().stream().map(n -> n.toUpperCase()).collect(Collectors.toList()))); nameCodes.addAll( germplasmUpdateDTOList.stream().map(o -> StringUtils.isNotEmpty(o.getPreferredName()) ? o.getPreferredName().toUpperCase() : null) .filter(Objects::nonNull).collect( Collectors.toSet())); final List<String> existingNamesCode = this.germplasmService.filterGermplasmNameTypes(nameCodes).stream().map(GermplasmNameTypeDTO::getCode).collect( Collectors.toList()); final Set<String> attributesCode = new HashSet<>(); germplasmUpdateDTOList.stream().filter(germ -> germ.getAttributes() != null).collect(Collectors.toList()) .forEach( g -> attributesCode.addAll(g.getAttributes().keySet().stream().map(n -> n.toUpperCase()).collect(Collectors.toList()))); final List<String> existingAttributesCode = this.germplasmService.filterGermplasmAttributes(attributesCode).stream().map(AttributeDTO::getCode).collect( Collectors.toList()); nameCodes.removeAll(existingNamesCode); attributesCode.removeAll(existingAttributesCode); if (!nameCodes.isEmpty()) { errors.reject("germplasm.update.invalid.name.code", new String[] {String.join(",", nameCodes)}, ""); } if (!attributesCode.isEmpty()) { errors.reject("germplasm.update.invalid.attribute.code", new String[] {String.join(",", attributesCode)}, ""); } final List<String> ambiguosCodes = new ArrayList<>(CollectionUtils.intersection(existingNamesCode, existingAttributesCode)); if (!ambiguosCodes.isEmpty()) { errors.reject("germplasm.update.ambiguous.code", new String[] {String.join(",", ambiguosCodes)}, ""); } }