Since Java doesn’t offer any character stream, we can create an IntStream (a stream of ints) of characters when dealing with strings. This post will discuss how to convert string to IntStream and IntStream to string in Java.

1. Convert String to IntStream

The simplest solution to get an IntStream from a string is to call the codePoints() or chars() method on the string, as shown below:

Download  Run Code

Output:

84
101
99
104
105
101
32
68
101
108
105
103
104
116

2. Convert IntStream to String

Converting IntStream to string is not so straightforward in Java. The idea is to use a collector, which accumulates the values using a StringBuilder.

Download  Run Code

Output:

Techie Delight

3. Convert Character array to IntStream

If we have a character array, we can convert it into a stream of Characters and then use flatMapToInt to convert the stream into an IntStream.

Download  Run Code

Output:

84
101
99
104
105
101

That’s all about converting IntStream to Java String and vice versa.