Java8 Stream - HashSet of Byte from IntStream

Viewed 825

I'm trying to create a HashSet<Byte> of bytes 1, 2, 3, ... 9 with the Java 8 Streams API. I thought using IntStream and then downgrading the values to byte would do it.

I'm trying variations of

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 those 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 flatMap or mapToObject?

2 Answers
Related