Want to create a stream of characters from char array in java

JavaJava 8Java Stream

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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]);

Solution 2 - Java

A way to do this is via a String object:

char[] list = {'a','c','e'};
Stream<Character> charStream = new String(list).chars().mapToObj(i->(char)i);

I like to do it this way because all the complexity of transforming the array is wrapped into the String creation, and the wrapping of char is also performed behind the scene for me so I can focus on the business logic.

Solution 3 - Java

A short and efficient way to create an IntStream from char[] array is to use java.nio.CharBuffer:

char[] list = {'a','c','e'};
IntStream stream = CharBuffer.wrap(list).chars();

This way you can use an IntStream interpreting the int values as characters. If you want a boxed Stream<Character> (which may be less efficient), use

Stream<Character> stream = CharBuffer.wrap(list).chars().mapToObj(ch -> (char)ch);

Using CharBuffer can be a little bit faster than IntStream.range as it has custom spliterator inside, so it does not have to execute an additional lambda (possibly as slow polymorphic call). Also it refers to the char[] array only once and not inside the lambda, so it can be used with non-final array variable or function return value (like CharBuffer.wrap(getCharArrayFromSomewhere()).chars()).

Solution 4 - Java

The simplest change you can make to the code is change char[] to Character[].

Another way is to create a new ArrayList of the boxed chars:

char[] list = {'a','c','e'};
List<Character> listArray = new ArrayList<>();
for (char c : list)
    listArray.add(c);
Stream<Character> cStream = listArray.stream();

In addition, you can use Google Guava's Chars class, to replace the for loop with:

List<Character> listArray = Chars.asList(list);

Solution 5 - Java

Getting substream of Characters

 String givenString = "MyNameIsArpan";
        
       Object[] ints = givenString.chars().mapToObj(i -> (char)i).toArray();

       String subString = Arrays.stream(ints,2,6).
                             map(i -> (char)i).
                             map(String::valueOf).
                             collect(Collectors.joining());

      System.out.println(subString);

OUTPUT : Name

Solution 6 - Java

Using Stream Builder:

String inputString = "This is string";
Stream.Builder<Character> charStreamBuilder = Stream.builder();
for (char i :inputString.toCharArray()) {
   charStreamBuilder.accept(i);
 }
 Stream<Character> characterStream = charStreamBuilder.build();

Solution 7 - Java

"ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().mapToObj(i -> (char) i);

Solution 8 - Java

How about a workaround: char[] list = {'a','c','e'};

Arrays.asList(list).stream();

Solution 9 - Java

I believe the simplest way to convert to Character stream is

    char[] list = {'a','c','e'};

    Stream<Character> characters = Stream.ofAll(list);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSangjin KimView Question on Stackoverflow
Solution 1 - JavaAlexis C.View Answer on Stackoverflow
Solution 2 - JavaLars HartviksenView Answer on Stackoverflow
Solution 3 - JavaTagir ValeevView Answer on Stackoverflow
Solution 4 - JavaTJ MazeikaView Answer on Stackoverflow
Solution 5 - JavaArpan SainiView Answer on Stackoverflow
Solution 6 - JavanavnathView Answer on Stackoverflow
Solution 7 - JavaFzumView Answer on Stackoverflow
Solution 8 - JavaAsif MalekView Answer on Stackoverflow
Solution 9 - JavaRakesh ChauhanView Answer on Stackoverflow