Converting String to Stream of chars – 将字符串转换为字符流

最后修改: 2017年 6月 18日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Java 8 introduced the Stream API, with functional-like operations for processing sequences. If you want to read more about it, have a look at this article.

Java 8引入了Stream API,具有处理序列的类似函数的操作。如果你想了解更多关于它的信息,请看一下这篇文章

In this quick article, we’ll see how to convert a String to a Stream of single characters.

在这篇快速文章中,我们将看到如何将一个String转换为一个单字符的Stream

2. Conversion Using chars()

2.使用chars()进行转换

The String API has a new method – chars() – with which we can obtain an instance of Stream from a String object. This simple API returns an instance of IntStream from the input String.

String API有一个新的方法 – chars() – 我们可以用它从String对象中获得Stream的实例。这个简单的API从输入的String中返回一个IntStream的实例。

Simply put, IntStream contains an integer representation of the characters from the String object:

简单地说,IntStream包含来自String对象的字符的整数表示。

String testString = "String";
IntStream intStream = testString.chars();

It’s possible to work with the integer representation of the characters without converting them to their Character equivalent. This can lead to some minor performance gains, as there will be no need to box each integer into a Character object.

我们可以使用字符的整数表示法,而不将它们转换为字符等价物。这可以带来一些小的性能提升,因为不需要将每个整数框入一个字符对象。

However, if we’re to display the characters for reading, we need to convert the integers to the human-friendly Character form:

然而,如果我们要显示这些字符以供阅读,我们需要将整数转换成对人类友好的字符形式。

Stream<Character> characterStream = testString.chars()
  .mapToObj(c -> (char) c);

3. Conversion Using codePoints()

3.使用codePoints()进行转换

Alternatively, we can use the codePoints() method to get an instance of IntStream from a String. The advantage of using this API is that Unicode supplementary characters can be handled effectively.

另外,我们可以使用codePoints()方法从String.中获得一个IntStream的实例。使用这个API的好处是,可以有效地处理Unicode补充字符。

Supplementary characters are represented by Unicode surrogate pairs and will be merged into a single codepoint. This way we can correctly process (and display) any Unicode symbol:

补充字符由Unicode代理对表示,并将被合并为一个代码点。这样,我们可以正确地处理(和显示)任何Unicode符号。

IntStream intStream1 = testString.codePoints();

We need to map the returned IntStream to Stream<Character> to display it to users:

我们需要将返回的IntStream映射为Stream<Character>,以便向用户显示。

Stream<Character> characterStream2 
  = testString.codePoints().mapToObj(c -> (char) c);

4. Conversion to a Stream of Single Character Strings

4.转换为单字符字符串

So far, we’ve been able to get a Stream of characters; what if we want a Stream of single character Strings instead?

到目前为止,我们已经能够得到一个字符的Stream;如果我们想要一个单字符的Streams呢?

Just as specified earlier in the article, we’ll use either the codePoints() or chars() methods to obtain an instance of IntStream that we can now map to Stream<String>.

正如文章前面所指定的,我们将使用codePoints()chars()方法来获得一个IntStream的实例,现在我们可以将其映射到Stream<String>

The mapping process involves converting the integer values to their respective character equivalents first.

映射过程包括首先将整数值转换为其各自的字符等价物。

Then we can use String.valueOf() or Character.toString() to convert the characters to a String object:

然后我们可以使用String.valueOf()Character.toString()来将字符转换成String对象。

Stream<String> stringStream = testString.codePoints()
  .mapToObj(c -> String.valueOf((char) c));

5. Conclusion

5.结论

In this quick tutorial, we learn to obtain a stream of Character from a String object by either calling codePoints() or chars() methods.

在这个快速教程中,我们学习通过调用codePoints()chars()方法从String对象中获得Character流。

This allows us to take full advantage of the Stream API – to conveniently and effectively manipulate characters.

这使我们能够充分利用Stream API–方便而有效地操作字符。

As always, code snippets can be found over on GitHub.

像往常一样,代码片段可以在GitHub上找到