I have a collection of objects of Class A
class A {
String code;
long timestamp;
long largestTimestamp;
}
I have to populate the largestTimestamp field for each object (the largest "timestamp" value in the group of objects with the same code). I can do this in two steps as follows -
Map<String, Long> largestTimestampMap = list.stream().collect(Collectors.toMap(A::getCode, A::getTimestamp, Long::max));
list.forEach(a -> a.setLargestTimestamp(largestTimestampMap.get(a.getCode())));
Is there a way to combine these into a single stream chain?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With