0

I actually wanted to replace this to method reference since sonar is throwing an issue ,I have also searched answers for similar tittle i was't able to find the solution.

    String type="test2"
    List<String> validSimtSwType = Arrays.asList("test1", "test2", "test3", "test4");
    if((validSimtSwType.stream().anyMatch(name -> type.contains(name)))){
             //statements 
         }
Lokeshkumar R
  • 575
  • 5
  • 13
  • 3
    First hit when googling "sonar replace lamda with method reference": [Sonar : Replace this lambda with a method reference](https://stackoverflow.com/questions/25606906/sonar-replace-this-lambda-with-a-method-reference) – Socowi Nov 07 '18 at 12:20
  • In your example, you are only looking for an exact match, which can be done much simpler, i.e. `validSimtSwType.contains(type)`. If you truly want sub-string matching, it’s potentially more efficient to use `if(validSimtSwType.stream().anyMatch(Pattern.compile(type, Pattern.LITERAL) .asPredicate())) { … }`. – Holger Nov 07 '18 at 12:47

1 Answers1

4

You can replace it with a method reference referencing the contains method of the type instance:

if((validSimtSwType.stream().anyMatch(type::contains)))
Eran
  • 387,369
  • 54
  • 702
  • 768