code smell sonar Replace this lambda with a method reference

126 Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

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();