从代码点数字的 IntStream 中创建一个字符串?

Make a string from an IntStream of code point numbers?

如果我正在使用 Java 流,并以 IntStream of code point numbers for Unicode characters, how can I render a CharSequence 结束,例如 String

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

我在几个接口和 类 上发现了一个 codePoints() 方法,它们都生成 IntStream 个代码点。然而我还没有找到任何接受相同的构造函数或工厂方法。

我正在寻找相反的:

➥ 如何从 IntStream 个代码点实例化 StringCharSequence 等?

使用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()
;

如果你喜欢更笼统的CharSequence interface over concrete String, simply drop the toString() at the end. The returned StringBuilderCharSequence.

IntStream codePointStream = "input_goes_here".codePoints ();
CharSequence output = codePointStream.collect ( StringBuilder :: new , StringBuilder :: appendCodePoint , StringBuilder :: append );

或更直接到 String 通过使用数组传递给 new String(…)

IntStream intStream = "input_goes_here".codePoints();

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


这是原始的简短解决方案,没有多余的 IntStream intStream 作业:

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

不要忘记 Java IO 库:
使用 IntStream::collect StringWriter

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.
        StringWriter::new,         // Supplier<R> supplier
        StringWriter::write,       // ObjIntConsumer<R> accumulator
        (w1, w2) -> w1.write(      // BiConsumer<R,R> combiner
            w2.toString() ) )
    .toString();