Java Program to convert Character Array to IntStream
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 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); } } |
chevron_right
filter_none
Output:
IntStream: 71 101 101 107 115
Recommended Posts:
- Program to convert String to IntStream in Java
- Program to convert IntStream to String in Java
- Convert a String to Character array in Java
- Program to convert Array to Set in Java
- Program to Convert Stream to an Array in Java
- Program to convert Array to List in Java
- Program to convert Primitive Array to Stream in Java
- Program to convert Boxed Array to Stream in Java
- Program to convert Byte Array to Writer in Java
- Program to convert first character uppercase in a sentence
- Program to Convert Set of Integer to Array of Integer in Java
- Java Program to get a character from a String
- Java program to count the occurrences of each character
- Program to Convert Set to List in Java
- Program to Convert List to Map in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



