Convert IntStream to String in Java

Java 8Object Oriented ProgrammingProgramming

If you have IntStream with ASCII values, then easily convert it to string using the below given example.

For IntStream class, import the following package

import java.util.stream.IntStream;

Let’s say the following is our IntStream

IntStream stream = "Example".chars();

Now, convert the IntStream to string

String str = stream.collect(StringBuilder::new,
   StringBuilder::appendCodePoint,
   StringBuilder::append).toString();

The following is an example to convert IntStream to String in Java

Example

import java.util.stream.IntStream;
class Demo {
   public static void main(String[] args) {
      IntStream stream = "Example".chars();
      System.out.println("The ASCII values...");
      String str = stream.collect(StringBuilder::new,
      StringBuilder::appendCodePoint,
      StringBuilder::append).toString();
      System.out.println("The converted string = " + str);
   }
}

Output

The ASCII values...
The converted string = Example
raja
Published on 28-Mar-2019 08:42:20
Advertisements