【发布时间】:2020-04-05 06:22:52
【问题描述】:
我正在尝试使用stream() 迭代一个列表并放入一个映射,其中键是蒸汽元素本身,值是 AtomicBoolean,true。
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()?
【问题讨论】:
-
streamDetails.stream().collect(Collectors.toMap(Function.identity(), v->new AtomicBoolean(true))); -
我认为你知道语法,这只是一个错字,因为你已经在同一个调用中为
FunctionalInterface使用了 lambda =>Collectors.toMap(x -> x.toString(), new AtomicBoolean(true)))
标签: java collections java-8 java-stream method-reference