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:
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
Character charArray[] = { 'G' , 'e' , 'e' , 'k' , 's' };
IntStream
intStream
= Stream
.of(charArray)
.flatMapToInt(IntStream::of);
System.out.println( "IntStream:" );
intStream.forEach(System.out::println);
}
}
|
Output:
IntStream:
71
101
101
107
115
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Dec, 2018
Like Article
Save Article