Sonar : Replace this lambda with a method reference

This code sample

Collection<Number> values = transform(
        getValuatedObjects(),
        input -> getValueProvider().apply(input).getValue());

violates the sonarqube rule:

Replace lambdas with method references when possible

is it a sonar bug ? or can i really use a method reference ?

You can’t replace the lambda input -> getValueProvider().apply(input).getValue() with a method reference without changing the semantics.

A method reference replace a single method invocation, so it can’t simply replace a lambda expression consisting of more than one method invocation.

A lambda expression of the form input -> getValueProvider().apply(input) could be replaced by getValueProvider()::apply if, and only if, the evaluation time of getValueProvider() does not matter as in the lambda form the method is invoked on each lambda body evaluation while for the method reference it is invoked only once and the result captured.

This is similar to the difference between x -> System.out.println(x) and System.out::println where reading the contents of the field System.out happens at different times but usually it doesn’t matter. But you should be aware of the difference.

In your example, a third method getValue() is invoked. The only way to express that with method references needs a functional interface like Function which has methods like andThen and/or compose. However, the way Java 8 works, that would require casting the first method reference to the target interface to invoke the combining method which would be by no way easier to read that the lambda expression you have now: ((Function<X,Y>)getValueProvider()::apply).andThen(Y::getValue) where Y is the type, apply(input) returns.

Note that the rule says "Replace lambdas with method references when possible" which gives you room to say, "well, here it is impossible", however, I’m not sure how much you can call it a "rule" then…

How to Convert a Lambda Expression to Method Reference in Java 8?, The simple rule to replace lambda expression with method reference is built on common sense, which you will learn in this article. If you look� This kind of method reference is called "Reference to an instance method of a particular object". In this case, you are referring to the method createSomeValue of the instance this . Whether it is "better" or not that using a lambda expression is a matter of opinion.

if you are coding in java 8 you can use method reference in place of lambda expression for code readable

List<Integer> list = Arrays.asList(1,2,3,4,5);

replace this lambda with a method reference

strList.stream().sorted().collect(Collectors.toList()).forEach(s -> System.out.println(s));

Replace

strList.stream().sorted().collect(Collectors.toList()).forEach(System.out::println);

Java java8: Lambdas should be replaced with method references, Note that this rule is automatically disabled when the project's sonar.java.source is lower than 8 . Noncompliant Code Example. class A { void process(List<A> list) � Replace this lambda with a method reference. Code is : protected List<Test> getTests(List<String> testIds) { List<Test> tests = new ArrayList<> (); if (!CollectionUtils.isEmpty(testIds)) { testIds.stream().forEach(eachTestId -> tests.add(getTest(eachTestId))); } return tests; } How can I get over this warning?

list.stream().sorted().collect(Collectors.toList()).forEach(element -> operate(element));

replace the above lambda with a method reference.

list.stream().sorted().collect(Collectors.toList()).forEach(this::operate);

Java: Lambdas should be replaced with method references, Note that this rule is automatically disabled when the project's sonar.java.source is lower than 8 . Noncompliant Code Example. class A { void process(List<A> list) � When I checked it in sonar, the result is: Replace this lambda with a method reference. AIDetailsDto aIDetailsDto = aaaService .getDetailsByUserId (userId) if (!ObjectUtils.isEmpty (aIDetailsDto)) { List<String> kvpValues = callService.getKVPCodes (NewConstants.REMOVED) .stream () .filter (s -> aIDetailsDto.getResult ().getIdNo ().equals (s)) I tried to change it however I get an error.

Replace Expression Lambda with Method Reference, This rule simplifies expression lambdas by using method reference. The rule can only be applied if the parameters of the lambda expression and the method� This kind of method reference is called "Reference to an instance method of a particular object". In this case, you are referring to the method createSomeValue of the instance this. Whether it is "better" or not that using a lambda expression is a matter of opinion.

Method References Cannot Always Replace Lambda Expressions , Method references require exact signatures in order to replace lambda expressions. In this sample Java API design, we see the limits of those� A method reference replace a single method invocation, therefore in the case of a lambda invoking multiple method it is not possible to replace it by a method reference and we have a false positive.

How to convert a lambda expression to method reference in Java 8 , Anyway, the simple rule to replace lambda expression with method reference is built on common sense, which you will learn in this article. How to replace lambda expression with method reference in Java 8. If you are using a lambda expression as an anonymous function but not doing anything with the argument passed, you can replace lambda expression with method reference. Below code is a good example to replace lambdas with method reference. listOfNumbers.stream().sorted().forEach(number ->{ System.out.println(number); });

Comments