我试图创建HashSet<Byte>的byte小号1, 2, 3, ... 9与Java 8流API.我想使用IntStream然后降级值byte就可以了.
我正在尝试变种
HashSet<Byte> nums = IntStream.range(1, 10).collect(Collectors.toSet());
HashSet<Byte> nums = IntStream.range(1, 10).map(e -> ((byte) e)).collect(Collectors.toSet());
但这些都不起作用.
Error:(34, 73) java: 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.Set<java.lang.Object>>
reason: cannot infer type-variable(s) R
(actual and formal argument lists differ in length)
Run Code Online (Sandbox Code Playgroud)
我需要做的flatMap还是mapToObject?
您需要使用mapToObj,因为HashSet和所有泛型都需要对象
Set<Byte> nums = IntStream.range(1, 10)
.mapToObj(e -> (byte) e)
.collect(Collectors.toSet());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
450 次 |
| 最近记录: |