I am trying to create HashSet<Byte>from byte 1, 2, 3, ... 9using the Java 8 Streams APIs. I thought of using IntStreamand then overriding the values to byte.
I'm trying to change
HashSet<Byte> nums = IntStream.range(1, 10).collect(Collectors.toSet());
HashSet<Byte> nums = IntStream.range(1, 10).map(e -> ((byte) e)).collect(Collectors.toSet());
But none of them work.
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)
Do I need to do flatMapor mapToObject?
source
share