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!

NEW: Online Workhop Effective WebApps without Frameworks is also coming to: MUC Airport.

Airport MUC workshops: Web (SPA, PWAs, Offline, Desktop, Mobile) Applications Essentials and Effective Web Applications. No migrations. #usetheplatform

Podcast: and newsletter:

A book about rethinking Java EE Patterns


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.


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

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