Nana Ba
Nana Ba

Reputation: 87

SONAR: Replace with lambda with a method reference with a string parameter

 List<String> list;
 //...add something in the list
String value = "anything";
boolean b = list.stream().anyMatch( element -> value.startsWith(element))

I get the sonar information about Replace with lambda with a method reference. But I have to call method reference on String ???

Upvotes: 2

Views: 3202

Answers (1)

Anton Balaniuc
Anton Balaniuc

Reputation: 11739

String value = "anything";
boolean b = list.stream().anyMatch( value::startsWith);

Or you don't even need to declare String value, you can use "anything" directly in the lambda expression:

list.stream().anyMatch( "anything"::startsWith)

Upvotes: 9

Related Questions