[英]Convert a String to a java.util.Stream<Character>
Sometimes I want to do something simple with each character in a string. 有时我想对字符串中的每个字符做一些简单的事情。 Unfortunately, because a string is immutable, there is no good way of doing it except looping through the string which can be quite verbose.
不幸的是,因为字符串是不可变的,除了循环遍历字符串之外没有好的方法可以做到这一点,这可能非常冗长。 If you would use a Stream instead, it could be done much shorter, in just a line or two.
如果你使用Stream代替它,它可以做得更短,只需要一两行。
Is there a way to convert a String
into a Stream<Character>
? 有没有办法将
String
转换为Stream<Character>
?
You can use chars()
method provided in CharSequence
and since String
class implements this interface you can access it. 您可以使用
CharSequence
提供的chars()
方法,因为String
类实现了此接口,您可以访问它。 The chars()
method returns an IntStream
, so you need to cast it to (char)
if you will like to convert IntStream
to Stream<Character>
chars()
方法返回一个IntStream
,因此如果要将IntStream
转换为Stream<Character>
需要将其IntStream
转换为(char)
Eg 例如
public class Foo {
public static void main(String[] args) {
String x = "new";
Stream<Character> characters = x.chars().mapToObj(i -> (char) i);
characters.forEach(System.out::println);
}
}
It's usually more safe to use stream of code points which is IntStream
: 使用
IntStream
代码点流通常更安全:
IntStream codePoints = string.codePoints();
This way Unicode surrogate pairs will be merged into single codepoint, so you will have correct results with any Unicode symbols. 这样,Unicode代理项对将合并为单个代码点,因此您可以使用任何Unicode符号获得正确的结果。 Example usage:
用法示例:
String result = string.codePoints().map(Character::toUpperCase)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
Also note that you avoid boxing, thus it might be even more effective than processing Stream<Character>
. 另请注意,您可以避免装箱,因此它可能比处理
Stream<Character>
更有效。
Another way to collect such stream is to use separate StringBuilder
: 收集此类流的另一种方法是使用单独的
StringBuilder
:
StringBuilder sb = new StringBuilder();
String result = string.codePoints().map(Character::toUpperCase)
.forEachOrdered(sb::appendCodePoint);
While such approach looks less functional, it may be more efficient if you already have a StringBuilder
or want to concatenate something more later to the same string. 虽然这种方法看起来功能较少,但如果您已经拥有
StringBuilder
或想要稍后将某些内容连接到同一个字符串,则可能会更有效。
You can use chars
method which returns IntStream
and by mapping it to char
you will have Stream<Character>
. 您可以使用返回
IntStream
chars
方法,并将IntStream
映射到char
您将拥有Stream<Character>
。 mapToObj
returns Object
valued Stream
in our case Stream
of Character
, because we have mapped the int
to char
and java Auto Boxed it to Character
internally . mapToObj
在我们的例子Stream
of Character
返回Object
值Stream
,因为我们已经将int
映射到char
并且java Auto将它封装到Character
内部 。
Stream<Character> stream = "abc".chars().mapToObj(c -> (char)c);
Moreover, with the help of guava ( com.google.common.collect.Lists
) you can use it like this, which return immutable list of Character
from the String
. 此外,在guava(
com.google.common.collect.Lists
)的帮助下,您可以像这样使用它,它从String
返回Character
不可变列表。
Stream<Character> stream = Lists.charactersOf("abc").stream();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.