How to Modify a Field in Each Element of List in Java 8 Using Stream

Modify property value of the objects in list using Java 8 streams

If you wanna create new list, use Stream.map method:

List<Fruit> newList = fruits.stream()
.map(f -> new Fruit(f.getId(), f.getName() + "s", f.getCountry()))
.collect(Collectors.toList())

If you wanna modify current list, use Collection.forEach:

fruits.forEach(f -> f.setName(f.getName() + "s"))

Java 8 streams - modifying all elements in a group

Yes, you can combine them into a single pipeline as follows:

list.stream()
.map(a -> new A(a.getCode(), a.getTimestamp(),
list.stream()
.filter(b -> a.getCode().equals(b.getCode()))
.mapToLong(A::getTimestamp)
.max().getAsLong()))
.collect(Collectors.toList());

or if you want to avoid creating a new list but instead modify the existing one like in your example then use replaceAll:

list.replaceAll(a -> new A(a.getCode(), a.getTimestamp(),
list.stream()
.filter(b -> a.getCode().equals(b.getCode()))
.mapToLong(A::getTimestamp)
.max().getAsLong()));

However, I'd recommend avoiding this approach:

1) because it has worse performance than your current implementation, this solution requires iterating over list again forEach element in it. Whereas the Map approach you've shown only requires a single get call and we all know how fast looking up in a map is.

2) There's more code.

Yes, you can refactor the code inside the map intermediate operation into a method to make it look shorter but ultimately it's still longer.

Sometimes the best approach requires two or more separate lines and in this case, it's best to proceed with your approach.

JAVA 8 : Change value inside stream

This should be corrrect since you aren't modifiying the list but the attribut of the element :

    besoins.stream().forEach(besoin -> {
String purchaseOrderPosition = besoin.getReferenceOa().trim();
if(!purchaseOrderPosition.isEmpty()) {
ValeursDynamiques valeursDynamiques = valeursDynamiquesService.DynamicValues(supplierNumber, purchaseOrderPosition);
besoin.setQuantityInTransit(valeursDynamiques.getUsedValues().getQteEnTransit());
besoin.setQuantityOrdered(valeursDynamiques.getUsedValues().getQteCommandee());
besoin.setQuantityDelivered(valeursDynamiques.getUsedValues().getQteRecue());
besoin.setDeliveryDateScheduled(valeursDynamiques.getUsedValues().getDateLivraisonPlanifiee());
besoin.setDeliverydateConfirmed(valeursDynamiques.getUsedValues().getDateLivraisonConfirmee());
besoin.setQuantityRestExpedited(valeursDynamiques.getUsedValues().getSoldeAExpedier());
}
}

you can directly use forEach() from list, and I have use the isEmpty() intead of != "".

But as previously stated why use a Stream here ? besoins.stream().forEach(besoin -> is less readable than for (Besoin besoin : besoins)

In general when prefer stream to filter, map, and extract some data

Modify Array List using condition in Java 8 Stream API

Your example code won't actually compile but i'll attempt to answer your question...

addonItems.stream().filter(addonItem -> GeoCode.TRP.toString().equals(addonItem.getSomeCode()))
.forEach(addonItem -> addonItem.setName("Got from GOOGLE"));

What this code does is stream the elements in the addonItems list and filter for addonItems based on our lambda filter expression. It will perform this stream operation when the forEach is called and set the name of each addonItem accordingly based on our filter expression.



Related Topics



Leave a reply



Submit