Get inspired by Java 1.8 !!

Keerthana Thiagaraj
5 min readMay 22, 2020

Let’s try to get a deeper understanding on a huge release of Java-1.8

There are many articles on Java-8 features. I was fond of writing one on the same after knowing its superpower!!

Java 8 introduced a new era of Java. There is an urge to find out the benefits of functional programming!. The below image gives us a visual effect of the features introduced.

Before we explore on the new features, let’s try to figure out what is it all about functional programming?. Ever since when Java 8 was introduced, it makes us to think about the problems and solutions in a different algorithmic way. To put in simple words,

In object-oriented programming, the primary abstraction is Class/Object, whereas in functional programming the primary abstraction is a function. As in OOP, objects form the building blocks for computation. Similarly, in functional programming, functions form the building blocks for computation.

That’s interesting!. But wait?🙄 Why do we need to go for Functional programming ?

  • One of the biggest advantages of functional programming is that since computations are expressed through functions, which do not change state or have any side effects and have the same output for each input,it fits exactly into the parallelism concept.
  • Moving forward, one can focus more on the problem rather than the code which becomes more concise and easy to read. Whereas, in OOP paradigm, one has to design solution being always concerned about the objects.

Now let’s try to find out what is a Functional interface?

Functional Interface:

It is an interface which consists of only one abstract method. And you will really wonder if you know that it is possible to instantiate functional interface. Amazing ! This is done with the help of Lambda Expression. We will deal with it in next stage.

Functional Interfaces are a key construct that allow Java, being an object-oriented platform, to support functional programming and to deal with functions.

There are many functional interfaces available in java.util.function package

Lambda Expression:

This forms the core part of Functional programming in Java. Some of the key benefits are as follows:

  • Lambda expressions are used to express a function directly in code without the need of object-oriented wrappers to support them.
  • Lambdas can replace anonymous classes which would result in clean and less verbose code.

There are 3 main parts of a Lambda expression:

1.A list of parameters

A lambda expression can have zero (represented by empty parentheses), one or more parameters:

    () -> System.out.println("Lambda Expression");
(String s) -> System.out.println(s);
(String s1, String s2) -> System.out.println(s1 + s2);

2.An arrow

Formed by the characters - and > to separate the parameters and the body.

3.A body

The body of the lambda expressions can contain one or more statements.

(s1,s2)->{
System.out.println(s1);
return s1+s2;
}

How is it related to functional interface?🤔 We can depict that the signature of the abstract method of a functional interface provides the signature of a lambda expression. Please refer here to get a clear picture about it!

Streams:

Streams allow defining a pipeline of operations that can carry out the transformation of input data into the required form. This is certainly suitable for performing bulk data operations on collections especially.

Rather than doing traditional external iteration, developers are provided a feasibility to do internal iteration.

Prior to Java-8 , we had to perform the below operation for finding out list of numbers greater than 2:

List<Integer> number = Arrays.asList(3, 1, 8, 2, 2);
List<Integer> result = new ArrayList<>();

for(Integer no:number){
if(no>2){
result.add(no);
}
}

As we can see above, following are some pitfalls:

  1. There is sequential process involved rather than parallel execution.
  2. Lot of code has to be written just for finding out a simple result.

In order to overcome the above limitations, Java 8 Stream API was introduced wherein we can implement internal iteration by using its several methods for filtering, mapping and displaying the result based on a given criteria.

Henceforth, the above code can be reduced as:

number.stream().filter(p->p>2).collect(Collectors.toList())

You can refer here for a detailed picture.

forEach() method:

Prior to Java-8, we need to use a loop for iterating the elements and then we need to write a separate logic for fetching our result. It’s so simple with a just a forEach() method wherein we are allowed to focus only on the business logic rather than writing loops!

Without forEach():

List<Integer> number = Arrays.asList(3, 1, 8, 2, 2);
for(Integer i:number){
System.out.println(i);
}

Using forEach():

number.forEach(System.out::println);

Refer here for a detailed usage.

Few more to go🏃🏼

Default and Static methods:

Here comes an interesting feature!. Prior to Java-8 , interfaces had only abstract methods whose implementation has to be provided in a separate class. So whenever a new method is introduced in an interface, its implementation has to be provided in all the classes that implement it.

In order to overcome this drawback, Java-8 has introduced the usage of default methods inside an interface. Amazing !

Here is how it looks like.

interface wowFeature{
default void show(){
System.out.println("Usage of Default Method");
}
}

Similarly static methods can also be placed inside interface.

***Don’t forget***:

In case if 2 of the implemented interfaces contain default methods with same method signature, the implementing class should explicitly specify which default method is to be used or it should override the default method.

Optional Class:

It is a public final class that is been introduced in order to avoid Null Pointer Exception. Please refer here for more details.

StringJoiner:

This is introduced to construct a sequence of characters separated by a delimiter. Now, you can create any string by passing delimiters like comma(,), hyphen(-) etc. You can also pass prefix and suffix to the character sequence. Please check here!

Date/Time API:

Java-8 has introduced several date and time packages including local date, local time. Another goodie is that, most of the methods are thread-safe. Please refer here for more info!

That is all for this article. Give a clap if you had enjoyed!

Do visit my YouTube channel and subscribe for interesting tech videos!🔄

--

--