将 IntStream 转换为 Map
2022-09-01 19:00:12
我有一个,并希望该流的每个元素进行一些计算并返回它们,因为键是int值,值是该计算的结果。我写了以下代码:IntStream
Map
IntStream.range(0,10)
.collect(
Collectors.toMap(Function.identity(), i -> computeSmth(i)));
哪里。我得到了下一个编译器错误computeSmth(Integer a)
method collect in interface java.util.stream.IntStream cannot be applied to given types;
required: java.util.function.Supplier<R>,java.util.function.ObjIntConsumer<R>,java.util.function.BiConsumer<R,R>
found: java.util.stream.Collector<java.lang.Object,capture#1 of ?,java.util.Map<java.lang.Object,java.lang.String>>
reason: cannot infer type-variable(s) R
(actual and formal argument lists differ in length)
我做错了什么?