Java Program to convert Character Array to IntStream
Last Updated :
11 Dec, 2018
Given a Character Array, the task is to convert this array into an IntStream containing the ASCII values of the character elements.
Examples:
Input: char[] = { 'G', 'e', 'e', 'k', 's' }
Output: 71, 101, 101, 107, 115
Input: char[] = { 'G', 'e', 'e', 'k', 's', 'F', 'o', 'r', 'G', 'e', 'e', 'k', 's' }
Output: 71, 101, 101, 107, 115, 70, 111, 114, 71, 101, 101, 107, 115
Algorithm:
- Get the Character Array to be converted.
- Convert it into Stream using Stream.of() method.
- Convert the formed Stream into IntStream using flatMapToInt() method.
- Print the formed IntStream.
Below is the implementation of the above approach:
Java
// Java program to convert
// Character Array to IntStream
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Get the Character Array to be converted
Character charArray[] = { 'G', 'e', 'e', 'k', 's' };
// Convert charArray to IntStream
IntStream
intStream
= Stream
// Convert charArray into Stream of characters
.of(charArray)
// Convert the Stream of characters into IntStream
.flatMapToInt(IntStream::of);
// Print the elements of the IntStream
System.out.println("IntStream:");
intStream.forEach(System.out::println);
}
}
Output:
IntStream:
71
101
101
107
115
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java