Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 streams - modifying all elements in a group

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?

like image 511
Oceanic Avatar asked Dec 01 '18 03:12

Oceanic


1 Answers

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.

like image 103
Ousmane D. Avatar answered Oct 09 '22 06:10

Ousmane D.