How to remove Sonar issue on Java stream "Refactor the code so this stream pipeline is used"

I’m working on a project (Java 17) where I have a list of object with two properties, actionId (String) and right (boolean). I’m trying to get the actionId for object with right = true and store the result as List of String.

This is my code:

 List<String> usserValidActionsArray =

My code works fine, but Sonar is blocking me by saying:

Refactor the code so this stream pipeline is used

Can anyone suggest to me how I can improve my code to be compliant?

>Solution :

I guess that Sonar doesn’t know about Stream.toList() (introduced in Java 16), and thinks it is a non-terminal operation. This seems to be bug SONAR-3746 (see also Stream#toList (JDK 16) is not recognized as terminal operator). This problem should be solved in SonarLint 7.2.

I guess, that means you need SonarQube 9 or higher, as SonarQube 9.0 is the first version that announced Java 16 support, and SonarQube 9.1 mentions adding new rules for Java 16. However, I’m not to familiar with how SonarLint and SonarQube updates interact in this way.

As a workaround, you can replace toList() with collect(Collectors.toList()).

Leave a Reply