Convert Character Array to IntStream in Java

Java 8Object Oriented ProgrammingProgramming

Let’s say the following is our character array:

Character arr[] = { 'V', 'e', 'h', 'i', 'c', 'l' , 'e' };

To convert the above character array to IntStream

IntStream stream = Stream.of(arr).flatMapToInt(IntStream::of);

We have used the flatMapToInt() method for this.

The following is an example to convert character array to IntStream in Java:

Example

 Live Demo

import java.util.stream.*;
public class Main {
   public static void main(String[] args) {
      Character arr[] = { 'V', 'e', 'h', 'i', 'c', 'l' , 'e' };
      System.out.println("The character array = ");
      for (char value : arr) {
         System.out.println("Value = " + value);
      }
      IntStream stream = Stream.of(arr).flatMapToInt(IntStream::of);
      System.out.println("\nCharacter Array to IntStream = ");
      stream.forEach(System.out::println);
   }
}

output

The character array =
Value = V
Value = e
Value = h
Value = i
Value = c
Value = l
Value = e
Character Array to IntStream =
86
101
104
105
99
108
101
raja
Published on 01-Apr-2019 12:24:25
Advertisements