Aggregation on list forEach and maps improvals I am using a function in a SpringBoot @RestController, with Lombok

This function is an intermediate result that permit me to do an aggregation between two classes.

This 2 classes are:

@Data
public class AchMatiereCmdCptAggDto implements Serializable {

    private static final long serialVersionUID = 4022154955808447185L;
    private String codService;
    private String codSociete;
    private String numCompte;
    private String codCommande;
    private Double mntCommandeHTLCY;
    private Double mntLivreHTLCY;
    private Double mntFactureHTLCY;
}

@Getter
@Setter
public class AchMatiereCompteCommandeAggFullDto extends AchMatiereCmdCptAggDto implements Serializable {

    private static final long serialVersionUID = 6357406952790844723L;
    private Date dateCommande;
    private String codFournisseur;
    private String libFournisseur;
    private Boolean booFournisseurIntragroupe;
    private Long idCommandeEtat;

}

public static String stringConcat(String ... elements) {
    return new StringBuilder().append(elements).toString();
}


I use them that way, and I want to improve forEach part, and any other improvable piece of code:

 public List&lt;AchMatiereCompteCommandeAggFullDto&gt; mergeAchat(List&lt;AchMatiereCompteCommandeAggFullDto&gt; achats,


List&lt;AchMatiereCmdCptAggDto&gt; achats2) {

    Set&lt;AchMatiereCmdCptAggDto&gt; setAchats = new HashSet&lt;&gt;(achats2);
    Map&lt;String, AchMatiereCmdCptAggDto&gt; map = setAchats.stream()
            .collect(Collectors.toMap(
                    p -&gt; stringConcat(p.getNumCompte(), p.getCodCommande()),
                    p -&gt; p, (ach1, ach2) -&gt; ach2
            ));

    achats.stream().forEach(achat -&gt; {
        String k = stringConcat(achat.getCodSociete(), achat.getCodService(), achat.getNumCompte(), achat.getCodCommande());

        if (map.containsKey(k)) {
            achat.setMntFactureHTLCY(map.get(k).getMntFactureHTLCY());
        }
    });
    return achats;
}


I was thinking about using a StringBuilder for the concatenation of attributes.

I would also like to avoid forEach, and use a Map or something like this.

Thanks for your help.
From java 8 it is available in the String class the method join that achieves the same result of your method stringConcat so instead of:

String k = stringConcat(achat.getCodSociete(), achat.getCodService(), achat.getNumCompte(), achat.getCodCommande());


You can use String.join with "" delimiter:

String k = String.join("", achat.getCodSociete(), achat.getCodService(), achat.getNumCompte(), achat.getCodCommande());


It is also possibile to avoid forEach and use Map instead but because you are modifying the achats list elements, you have to map every element of the list to a new instance of your class and after you obtain your new list. To achieve this you can define a new copy constructor for the class:

achats.stream()
    .map(original -&gt; {
        String k = //omitted for brevity : see above the code for k
        AchMatiereCompteCommandeAggFullDto copy = new AchMatiereCompteCommandeAggFullDto(original);
        if (map.containsKey(k)) {
            copy.setMntFactureHTLCY(map.get(k).getMntFactureHTLCY());
        }
        return copy;
    }).collect(Collectors.toList()); //&lt;- return the new list