Convert Character Array to IntStream in Java


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("
Character 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

Updated on: 30-Jul-2019

300 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements