Java 8

Java 8 is a powerful release of Java Platform, give us a big of change from JVM to language and libraries.

Lambda Expressions

Lambda Expressions is a new feature of Java 8 that makes Java code more clearly and easily for development.


Argument List Arrow Token Body Note
(arg1, arg2…) -> { body }
(int a, int b) -> a + b with type declaration
(a, b) -> a + b no type declaration
(int a, int b) -> {return (a + b)} with return statement
a -> a*a*a no parenthesis
() -> 42 no arguments
(String s) -> {System.out.print(s);} returns nothing

– A lambda expression can have zero, one or more parameters.
– No need to declare type of a parameter.
– No need to write parenthesis around parameter if there is only one parameter (required if more).
– No need to use curly braces in expression body if there is only one statement (required if more).
– No need to use return keyword if there is only one statement. The compiler returns the value automatically.

>>> More details at: Java 8 – Lambda Expressions

Functional Interfaces

Functional Interfaces is an interface with only one abstract method inside.

@FunctionalInterface annotation is used to annotate for Functional Interface. So compiler will throw errors when the interface is not a valid Functional Interface.

Invalid '@FunctionalInterface' annotation; MyFuncInterface is not a functional interface

Apply Functional Interfaces:

For Functional Interface with only one method, we can make code lean and beautiful with Lambda Expression, it seem like that we pass a method as argument to a function.

>>> More details at: Java 8 – Functional Interfaces

Method References

Method reference points to the method by its name by replacing a single-method lambda expression.
The syntax of a Method reference is: Object/Class/Type :: methodName

For example, the lambda expression:

can be replaced with the method reference:

There are four kinds of method references:
– Reference to a static method: ContainingClass::staticMethodName
– Reference to an instance method of a particular object: containingObject::instanceMethodName
– Reference to an instance method of an arbitrary object of a particular type: ContainingType::instanceMethodName
– Reference to a constructor: ClassName::new

>>> More details at: Java 8 – Method References


A stream is an abstract concept that represents a sequence of objects created by a source, it’s neither a data structure nor a collection object where we can store items. So we can’t point to any location in the stream, we just interact with items by specifying the functions.

Run the code above, the console shows:

Now, we have concept of using a Stream is to enable functional-style operations on streams of elements. Those operations are composed into a stream pipeline which consists of:
Source > Intermediate Operations > Terminal Operation
– a source (in the example, it is a collection – List, but it is also an array, a generator function, an I/O channel…)
intermediate operations (which transform current stream into another stream at the current chain, in the example, filter is the first operation and map is the second one)
– a terminal operation (which produces a result or side-effect, in the example, it is collect)

>>> More details at: Java 8 – Streams

Optional Class

Null reference causes many problems because it often denotes the absence of a value. Java 8 Optional is a new class that can help us handle these cases instead of checking null.

Instead of Null checking before doing something:

We can use Optionnal ifPresent() method alternatively:

>>> More details at: Java 8 – Optional

New Date/Time API

Java 8 provides lots of new Date Time APIs:
– Local Date – Time
– Plus and Minus Date – Time
– TemporalAdjusters
– Period & Duration
– TimeZone
– Formatting and Parsing

>>> More details at: Java 8 – Date Time


Java 8 Base64 provides a standard way to do Base64 encoding and decoding.
There are three types of Base64 encoding:
Basic: Encoder produces a set of characters within A-Za-z0-9+/. Decoder rejects any character NOT mapped to A-Za-z0-9+/.
URL: Encoder produces a URL or safe filename which is set of characters within A-Za-z0-9+_.
MIME: Output is mapped to MIME friendly format.

>>> More details at: Java 8 – Base64

Multithreading – CompletableFutures

Java 8 multithreading programming has an important feature named CompletableFuture. Java has the concept of a Future object, which can help program make something done while waiting for other things. However, although program can inspect Future objects to see if they’re done, what if program want to execute some code whenever its ready? It still has to wait until the result is available. CompletableFuture meets the requirement, and more than that.

Using one of those factory methods to create a CompletableFuture object:

If we don’t provide any Executor object as input parameter, the method will use ForkJoinPool.commonPool()

In this example, we use that global, general purpose pool ForkJoinPool.commonPool():

>>> More details at:
Java 8 CompletableFutures
Java 8 Multiple CompletableFutures
Java 8 CompletableFuture Handle

Java 8 – Misc

Java Read Text File by BufferedReader, Java 7,Java 8
How to use Java 8 Encode (Decode) an Image to Base64
Java 8 – How to convert List to Map & Map to List
How to use String Joiner with Java 8
Ways to convert an InputStream to String
How to compare Dates in Java

All articles: Java 8