On 05.02.2025 19:44, axh wrote:
Hi,
I want to ask about the commits "replace lambda with method referenceā. Some
are definitely the way to go, like this:
List<NormalXReference> normalXReferences = getXRefEntries().stream()
//
- .filter(e -> e instanceof NormalXReference) //
+ .filter(NormalXReference.class::isInstance) //
.map(NormalXReference.class::cast) //
.sorted() //
.collect(Collectors.toList());
But what is the benefit of this?
The code looks a bit cleaner IMHO because there's only one operation per
line. I didn't test whether using three map commands makes it slower,
but this is just a unit test. It takes less than 3 seconds.
I tested it now, without jmh, we may lose 0.2 seconds.
Tilman
return tokens.stream() //
- .filter(t -> t instanceof COSString) //
- .map(t -> ((COSString) t).getString().trim()) //
+ .filter(COSString.class::isInstance) //
+ .map(COSString.class::cast) //
+ .map(COSString::getString) //
+ .map(String::trim) //
.collect(Collectors.toList());
IMHO the new code is both longer and harder to read and will probably perform
worse than the lambda version because now three stream operations have to be
made instead of a single one. I know my IDE will also suggest this as a
refactoring, but I see no gain in it (apart from maybe getting rid of a warning
because of the cast, depending on how smart the static analysis is to determine
all elements that pass the filter operation are in fact COSString instances.
I think the overhead of creating the lambda is probably smaller than that of
the additional stream operations. If you are interested, I could maybe do a
small jmh benchmark of this on the weekend to be sure.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: dev-h...@pdfbox.apache.org