我已经编码了:
this.referenceService.get(id)
.map(Reference::hashCode)
.map(Integer::toString);
我收到此编译错误:
Ambiguous method reference: both toString() and toString(int) from the type Integer are eligible
我该如何解决这个问题?
最佳答案
您有两种可能的解决方案:
用 lambda 替换它:
this.referenceService.get(id)
.map(ref-> Integer.toString(ref.hashCode()));
this.referenceService.get(id)
.map(Reference::hashCode)
.map(Objects::toString); // this will cal toString method on you hash
编写自己的方法:
this.referenceService.get(id)
.map(this::toHashString);
private Strign toHashString(Reference ref) {
return Integer.toString(ref.hashCode());
}
关于java可选接口(interface)方法: Ambiguous method reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54513279/