All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.java.S4034.html Maven / Gradle / Ivy

There is a newer version: 8.6.0.37351
Show newest version

Why is this an issue?

When using the Stream API, call chains should be simplified as much as possible. Not only does it make the code easier to read, it also avoid creating unnecessary temporary objects.

This rule raises an issue when one of the following substitution is possible:

Original Preferred

stream.filter(predicate).findFirst().isPresent()

stream.anyMatch(predicate)

stream.filter(predicate).findAny().isPresent()

stream.anyMatch(predicate)

!stream.anyMatch(predicate)

stream.noneMatch(predicate)

!stream.anyMatch(x -> !(...))

stream.allMatch(...)

stream.map(mapper).anyMatch(Boolean::booleanValue)

stream.anyMatch(predicate)

Noncompliant code example

boolean hasRed = widgets.stream().filter(w -> w.getColor() == RED).findFirst().isPresent(); // Noncompliant

Compliant solution

boolean hasRed = widgets.stream().anyMatch(w -> w.getColor() == RED);




© 2015 - 2024 Weber Informatics LLC | Privacy Policy