How do I convert a string to a Java 8 character stream?

I found that this question is about getting Java from a Java string util. streams. Intstream, but I haven't found this method yet. I use Java 8

Fix: as you pointed out, I use Java 7 Now the method chars () is there But the question still applies:

How do I get a stream < character > from a string?

Solution

I want to point out the topic of my earlier answer, but it turns out that you have linked to that question Other answer also provides useful information

If you need a char value, you can use string Chars() returns intstream and converts int value to char without losing information Other answers explain why the stream class does not specialize in the charstream primitive

If you really want @ r_ 471_ 2419@ed Character object, then use maptoobj() to convert intstream to a stream of reference type In maptoobj(), convert the int value to char Since an object is expected as the return value here, char will be automatically loaded with a character This results in a flow < character > For example,

Stream<Character> sch = "abc".chars().mapToObj(i -> (char)i);
sch.forEach(ch -> System.out.printf("%c %s%n",ch,ch.getClass().getName()));

a java.lang.Character
b java.lang.Character
c java.lang.Character
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>