Make a string from an IntStream of code point numbers?

If I am working with Java streams, and end up with an IntStream of code point numbers for Unicode characters, how can I render a CharSequence such as a String?

String output = "input_goes_here".codePoints(). ??? ;  

I have found a codePoints() method on several interfaces & classes that all generate an IntStream of code points. Yet I have not been able to find any constructor or factory method that accepts the same.

I am looking for the converse:

➥ How to instantiate a String or CharSequence or such from an IntStream of code points?

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Use IntStream::collect with a StringBuilder. String output = "input_goes_here" .codePoints() // Generates an `IntStream` of Unicode code points, one `Integer` for each character in the string. .collect( // Collect the results of processing each code point. StringBuilder::new, // Supplier<R> supplier StringBuilder::appendCodePoint, // ObjIntConsumer<R> accumulator StringBuilder::append // BiConsumer<R,​R> combiner ) .toString() ; If you prefer the more general CharSequence interface over concrete String, simply drop the toString() at the end. The returned StringBuilder is a CharSequence. IntStream codePointStream = "input_goes_here".codePoints (); CharSequence output = codePointStream.collect ( StringBuilder :: new , StringBuilder :: appendCodePoint , StringBuilder :: append );

Jason
Guest

or more direct to String by using an array passed to new String(…)

IntStream intStream = "input_goes_here".codePoints();

int[] arr;
String output = new String( (arr = intStream.toArray()), 0, arr.length );

and here’s the original short solution without the superfluous IntStream intStream assignment:

int[] arr;
String output = new String( (arr = "input_goes_here".codePoints().toArray()), 0, arr.length );