我对该Function.identity()方法的用法有疑问.
想象一下以下代码:
Arrays.asList("a", "b", "c")
.stream()
.map(Function.identity()) // <- This,
.map(str -> str) // <- is the same as this.
.collect(Collectors.toMap(
Function.identity(), // <-- And this,
str -> str)); // <-- is the same as this.
Run Code Online (Sandbox Code Playgroud)
是否有任何理由你应该使用Function.identity()而不是str->str(反之亦然).我认为第二种选择更具可读性(当然是品味问题).但是,有没有"真正的"理由为什么应该首选?
这是我的代码:
Map<String, Collection<? extends String>> test = listOfTipusIdentificadorPacient.stream()
.collect(Collectors.groupingBy(
TipusIdentificadorPacient::getOid,
Collectors.mapping(TipusIdentificadorPacient::getUse, Collectors.toList())
)
);
Run Code Online (Sandbox Code Playgroud)
我收到此编译消息:
类型不匹配:无法从 Map<String,List> 转换为 Map<String,Collection<? 扩展字符串>>
我不太清楚如何覆盖Collectors.mapping以便:
return:
Map<String,Collection<? extends String>>
instead of:
Map<String,List<String>>
Run Code Online (Sandbox Code Playgroud)
我试图创建另一个通用代码以使其能够编译。
代码是:
return:
Map<String,Collection<? extends String>>
instead of:
Map<String,List<String>>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?