Java 8: Streaming A String


    public static void main(String[] args) {
        "hey duke".chars().forEach(c -> System.out.println((char)c));
    }


The parallel version does not preserve the order (and comes with additional overhead):


    public static void main(String[] args) {
        "hey duke".chars().parallel().forEach(c -> System.out.println((char) c));
    }


Enjoy Java 8!

Comments:

You should use forEachOrdered() to preserve the order in parallel stream.

Posted by Eng.Fouad on March 28, 2014 at 08:52 PM CET #

Hi Adam,

thanks for your post.
I wonder if there is 'nice and short' alternative to reduce stream of chars into String.
Because there is String(char[] value) constructor, but nothing I can do with streams.

Only stream.reduce(..) but it will be looking too bulky.

Thanks

Posted by Igor on May 05, 2014 at 03:44 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License