Replace this lambda with a method reference [duplicate]

  • A+

I have the following code. Sonar is complaining replace this lambda with a method reference.

Stream.iterate(0, i -> i + 1).limit(100).map(i -> Integer.toString(i)); 

If I replace it with it code below, it does not compile with compilation error: Type mismatch: cannot convert from Stream<Object> to <unknown>.

Stream.iterate(0, i -> i + 1).limit(100).map(Integer::toString); 

How is Integer::toString converting Stream<Object> to <unknown>?


It's ambiguous because the static and non-static toString() methods are both compatible with the functional signature Integer -> String. You can use String::valueOf instead.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: