获取使用方法引用在Java中8收藏家toMap方法的流对象

Saurav欧嘉:

我使用的尝试迭代名单stream()和投入的地图,其中关键是蒸汽元素本身,和值是的AtomicBoolean,真实。

List<String> streamDetails = Arrays.asList("One","Two");
toReplay = streamDetails.stream().collect(Collectors.toMap(x -> x.toString(), new AtomicBoolean(true)));

我得到了下面的错误在编译时。

Type mismatch: cannot convert from String to K
The method toMap(Function<? super T,? extends K>, Function<? super T,? extends U>) in the type Collectors is not applicable for the arguments ((<no type> x) -> {}, 
     AtomicBoolean)

还有什么比我做错了,我拿什么来取代我x -> x.toString()用?

ernest_k:

new AtomicBoolean(true)是无效的第二个参数的表达式Collectors.toMap

toMap这里想一Function<? super String, ? extends AtomicBoolean>(意流元素字符串)转换(或键入您预期的类型,的AtomicBoolean的映射值),以及正确的说法可能是:

Collectors.toMap(x -> x.toString(), x -> new AtomicBoolean(true))

这也可以用书面Function.identity

Collectors.toMap(Function.identity(), x -> new AtomicBoolean(true))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章