Java 8 Features With Examples
By AmarSivas | | Updated : 2021-03-12 | Viewed : 8450 times

Java 8 was released in March 2018 with different types of features using which we can resolve some of the problems in earlier releases. We will review all the important Java 8 features here.
Table of Contents:
Functional Interfaces
public interface MyInterface {
public void method();
}
@FunctionalInterface
public interface MyFunInterface {
public void method();
default void defaultMethod() {
// implementation
}
}
In Interface it has only unimplemented methods whereas FunctionalInterface has many numbers of implemented methods. So it is easy to add any number of methods which is not impacted on the subclasses. Here we can write the static default methods also.
@FunctionalInterface
public interface MyFunInterface1 {
public void method();
default void defaultMethod() {
// implementation
}
static void staticMethod() {
// implementation
}
}
Default And Static Methods
Default Methods
Functional Interface is an interface with at least one default interface. It has introduced in Java 8 to allow the developers to extend the interface functionality.
So here developers can introduce new methods where the newly added method is not required to implement in the child classes.
public interface MyInterface {
public void method();
}
public class MyClass implements MyInterface {
public void method();
}
Now it is time to add one more method in the MyInterface where the problem comes into the picture. suppose a new method added as given below.
public interface MyInterface {
public void method();
public void method1();
}
Now the method method1() is required to implement in all the child classes. But we do not want to implement the method1(). To handle this problem the default method of the Functional Interface is introduced.
@FunctionalInterface
public interface FunInterface {
void method(String name);
default void defaultMethod(String name) {
System.out.println("Default Method::" + name);
}
}
Static Methods
These methods are the same as default methods in FunctionalInterface. But these static methods can not be overridden.
@FunctionalInterface
public interface FunInterface {
void method(String name);
default void defaultMethod(String name) {
System.out.println("Default Method::" + name);
}
static void staticMethod(String name) {
System.out.println("static Method::" + name);
}
}
forEach() Method
This is method is introduced as a default method in the Iterable and Stream interfaces to iterate the collection elements. It has one argument that is the Consumer object. The business logic will be separately defined by using this Consumer Class. Please find the below-given example which is demonstrated forEach() method
package com.docsconsole.foreach;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class ForEachExample {
public static void main(String[] args) {
List<String> monthsList = new ArrayList<String>();
monthsList = Arrays.asList("January", "Jan", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December");
monthsList.forEach(new Consumer<String>() {
public void accept(String month) {
System.out.println(month);
}
});
Month month = new Month();
monthsList.forEach(month);
}
}
class Month implements Consumer<String> {
public void accept(String month) {
}
}
Lambda Expressions
You might observe that the Functional Interface has at least one default method. But it has also the abstract functions. So Lambda Expression will be used for the implementation of these abstract functions only. For a clear understanding of the concept of Lambda Expression, consider the below given anonymous function in MathOperations interface.
package com.docsconsole.lambdaexp;
public interface MathOperations {
public int add( int a, int b);
}
package com.docsconsole.lambdaexp;
public class LambdaExpExample {
public static void main(String[] args) {
MathOperations operation = (a, b) -> a + b;
System.out.println(operation.add(20, 10));
}
}
So if you observe the above function implementation we used the lambda expression to implement the abstract function.
Java Streams
In Java 8 To handle the collections Streams are used. The Stream can be executed Collections parallelly or sequentially.
Streams have a variety number of methods to process the collections sequentially or parallelly. Please find the below demonstrated program for Java Streams.
package com.docsconsole.javastrems;
import java.util.ArrayList;
import java.util.List;
public class JavaStreamsExample {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 20; i++) {
list.add(i);
}
//Sequential processing
list.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
//parallel processing
list.parallelStream().filter(n -> n % 2 == 0).forEach(System.out::println);
}
}