Search code examples
javalambdasonarqubemethod-reference

code smell sonar Replace this lambda with a method reference


Sonar is throwing code smell at the below lines:

File file = new File(xxxxxx);
Arrays.stream(file.listFiles()).map(path->path.getName()).collect(Collectors.toList()).toString();

I already converted using Steam, but it still shows code smell and asks for method reference.

How can I rewrite the above code?


Solution

  • Your attempts are wrong because the lambda function is the one with the -> operator, so the correct change is:

    Arrays.stream(file.listFiles()).map(File::getName).collect(Collectors.toList()).toString();