0

I've coded this:

this.referenceService.get(id)
    .map(Reference::hashCode)
    .map(Integer::toString);

I'm getting this compilation error:

Ambiguous method reference: both toString() and toString(int) from the type Integer are eligible

How could I work around this?

Jordi
  • 20,868
  • 39
  • 149
  • 333

1 Answers1

4

You have two possible solutions:

Replace it with a lambda:

this.referenceService.get(id)
    .map(ref-> Integer.toString(ref.hashCode()));

use Objects.toString()

this.referenceService.get(id)
    .map(Reference::hashCode)
    .map(Objects::toString); // this will cal toString method on you hash

Write your own method:

this.referenceService.get(id)
    .map(this::toHashString);

private Strign toHashString(Reference ref) {
  return Integer.toString(ref.hashCode());
}
Beri
  • 11,470
  • 4
  • 35
  • 57
  • @Eran Thank you, I have edited my response. – Beri Feb 04 '19 at 09:40
  • Could I chain `.map(Objects::toString(Reference::hashCode))`? – Jordi Feb 04 '19 at 10:01
  • No, to a Map you can pass a single method reference. (Objects::toString(...)) is an invalid method reference. Will not compile. But you can write your own method, and reference it. I have updated my answer, please check it. – Beri Feb 04 '19 at 10:03