If you want to set a value on all products in all productGroups, you will need to visit each one. How many? Well if you have x productGroups and y products in each productGroup (for argument's sake) then you would need to visit x * y products. No way around that. You could make the code a bit more concise and readable though, which is usually way more important anyway:
for(ProductGroup productGroup : productGroups) {
AmountEntity storageValue = productGroup.getStorageValue();
for(Product product : productGroup.getProducts()) {
product.setQuantity(storageValue);
}
}
or in lambda form:
productGroups.forEach(pg -> {
AmountEntity storageValue = pg.getStorageValue();
pg.getProducts().forEach(p -> p.setQuantity(storageValue));
});
Note that each product in a productGroup gets the same storageValue so you can define that variable in the outer loop as shown above. If you can change the domain model, you could ask yourself why the storageValue needs to be copied from the productGroup to each product anyway since this is basically redundant information, but maybe there is a good reason for that.
If I saw logic like this I would not worry about the performance initially. Calling getters and setters is usually not a performance bottleneck. Only if you do this for millions and millions of products it might take some time. In that case the solution would not lie in speeding up these iterations, but in reducing the fetched number of products (e.g. by paging).
O(n2)
anyways -- or you could update the logic inside the class Product to return as quantity the value ofstorageValue
-- ... as for code simplicity you could reduce it, maybe by using some lambda expressionsAmountEntity storageValue = ...
in to the outer loop to save some time but otherwise the complexity will still beO(n^2)
.List
implementations doproductGroups
andproducts
use? If it's e.g. aLinkedList
, you get another hidden O(n), resulting in O(n³), from theget(i)
calls.