From a char array, I want to construct a stream to use java 8 features such as filters and maps.
char[] list = {'a','c','e'}; Stream<Character> cStream = Stream.of(list); // Stream<Character> cStream = Arrays.stream(list);
The first method does not work (Reason: change cStream to Stream<char[]>
). The commented line does not also work (Reason: The method stream(T[])
in the type Arrays is not applicable for the arguments (char[]
)).
I know that if char[] list
is changed to int[]
, everything works fine using IntStream
. But I do not want to convert every char[]
to int[]
each time or change into a list when I need to use stream library on char
array.
You can use an IntStream
to generate the indices followed by mapToObj
:
char[] arr = {'a','c','e'}; Stream<Character> cStream = IntStream.range(0, arr.length).mapToObj(i -> arr[i]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With