Java8 stream – HashSet of bytes from intstream
•
Java
I'm trying to create a HashSet < byte > bytes 1, 2, 3,... 9 using the Java 8 streams API I want to use intstream and demote the value to byte
I'm trying a variant
HashSet < bytes > num = intstream range(1,10). collect(Collectors.toSet());
HashSet < bytes > num = intstream range(1,10). map(e – >((byte)e)). collect(Collectors.toSet());
But none of this worked
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?
Solution
You need to use maptoobj because HashSet and all generics require objects
Set<Byte> nums = IntStream.range(1,10) .mapToObj(e -> (byte) e) .collect(Collectors.toSet());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码