Java 8 - 'Hello World!' with Lambdas and Streams

Posted: 2015-08-22

  I needed a way to verify a certain Maven configuration worked correctly. 'Worked correctly' just meant that all Java files in all projects were found and compiled. The Java files were just 'Hello World' apps. At first, I just had a call to System.out.println() as the 'Hello World' implementation. Then I remembered that I has just learned the basics of Java 8 Lambdas and Streams.

So I went about seeing how short/concise I could get 'Hello World' implemented with Java 8 Lambdas and Streams.

Here is what I came up with:

        String message = "Hello Lambdas and Streams World!";

        // not so functional implementation with long notation (the curly braces)
                .forEach( i ->
                    System.out.print( (char) i  );
                } );
        // using mapping with long notation and a method reference to print() in System.out
                .mapToObj( i -> { return (char) i; } )

        // using mapping, short notation, and the method reference again
                .mapToObj( i ->  (char) i  )

The full source code is on Github: SecondGrandchildMain

Java 8 Features

Here are some good reads on Lambdas and Streams and other Java 8 features:

Date Created: 2015-08-22 09:47:41 -0500 (Sat, 22 Aug 2015)

back    |    top