Java 8 Features with an Example
Table of Contents
Lamda Expression
Java 8 provides Lamda Expression. Lamda is an expression where user write a code in functional style. It provides a way to implement Single Abstract Method by using an expression.
interface MyInterface {
void abstractFunc(int x,int y);
default void defaultFun() {
System.out.println("This is default method");
}
}
public class LamdaDemo {
public static void main(String args[]) {
//lambda expression
MyInterface myInterface = (int x, int y)->System.out.println(x+y);
System.out.print("Result = ");
myInterface.abstractFunc(5,5);
myInterface.defaultFun();
}
}
Method References
Java 8 provides Method reference. Method Reference is similar to Lambda just way to write is different. Each time when you write Lambda expression you can replace it with Method Reference.
import java.util.Optional;
interface ParentInter {
void display();
}
class DerievedInter{
public void display(){
System.out.println("Derived class Method");
}
}
class Main{
public static void main(String[] args){
DerievedInter derievedInter = new DerievedInter();
ParentInter parentInter = derievedInter::display;
parentInter.display();
}
}
Functional Interface
Java 8 provides Functional Interface. Functional Interface contains only one abstract method. It can have any number of default and static methods. It can also declare methods of object class.
Functional interfaces are also known as Single Abstract Method Interfaces (SAM Interfaces).
@FunctionalInterface
public interface FunctionalInterface_one
{
public void firstIntMethod();
@Override
public String toString();
@Override
public boolean equals(Object obj);
}
Optional Class
Java 8 provides a new class Optional. It is a public final class which is used to deal with NullPointerException. We need to import java.util package to use this class. It provides methods to check the presence of value for particular variable.
import java.util.Optional;
public class Main{
public static void main(String[] args) {
//Here is Empty List
List<String> listStr = new ArrayList<>();
Optional<String> checkNull =
Optional.ofNullable(listStr.get(0));
if (checkNull.isPresent()) {
String word = listStr.get(0).toLowerCase();
System.out.print(word);
} else
System.out.println("String is null");
}
}
Default and Static method
Java 8 provides default and static method Implementation in Interface. Default methods mainly enable Lambda Expression functionality.
import java.util.Optional;
interface ParentInter {
default void defaultMethod(){
System.out.println("I am default method of interface");
}
}
class DerivedInter implements ParentInter{
}
class Main{
public static void main(String[] args){
DerivedInter derivedInter = new DerievedInter();
derivedInter.defaultMethod();
}
}
Stream API
Java 8 provides Stream API. Stream API used to perform operation on Collections of Obj. Import java.util.stream package consists of classes, interfaces and an enum to allow functional-style operations on the elements.
Date Time API
Java 8 provides Date and Time API. The java.time package contains Java 8 Date and Time classes.
Important classes in Date Time API are :
- The LocalDate class defines a date. It has no representation for time or time-zone.
- The LocalTime class defines a time. It has no representation for date or time-zone.
- The LocalDateTime class defines a date-time. It has no representation of a time-zone.