Reading Time: 3 minutes
Let’s have a brief look at these Java 8 features. I will provide some code snippets for better understanding the features in a simple way.
Some of the important Java 8 features are;
- Lambda expressions
- Method references
- Functional Interfaces
1. Lambda expressions
- Lambda expression helps us to write our code in a functional style.
- These are just like functions and they accept parameters just like functions.
- It is an anonymous function, no function name and no method return type.
- The body of a lambda expression can contain zero, one or more statements.
- It saves a lot of code, we don’t need to define the method again for providing the implementation.
Syntax:
lambda operator -> body
Example:
() -> System.out.println("lambda expression simplest example");
2. Method references
- Method reference is used to refer to a method of functional interface.
- It is a compact and easy form of lambda expression.
- Each time when you are using lambda expression to just refer to a method, you can replace your lambda expression with method reference.
Types of method References are given below:-
1. Method reference to static method
@FunctionalInterface
public interface Walkable{
public abstract void walk();
}
public class Sample {
public static void walking() {
System.out.println(“Hey, lets go to walk”);
}
}
public class MainClass {
public static void main(String[] args) {
//Lambda Expression
//Walkable walkable=()->System.out.println(“Hey, lets go to walk”);
// Replaced above Lambda Expression with Method Reference
Walkable walkable=Sample::walking;
walkable.walk();
}
}
Output:
Hey, lets go to walk
2. Method reference to instance method of a particular object
@FunctionalInterface
public interface Welcome{
String welcome(String name);
}
public class Sample {
public String welcomeMessage(String name) {
return “Hey:” + name;
}
}
public class MainClass {
public static void main(String[] args) {
//Lambda Expression
// Welcome welcome2=name -> “Hey:” + name;
// Replaced above Lambda Expression with Method Reference
Welcome welcome2=new Sample()::welcomeMessage;
System.out.println(welcome2.welcome(“ABC”));
}
}
3. Method reference to an instance method from class Type
public class Main {
public static void main(String[] args) {
List<String> nameList=new ArrayList<>();
nameList.add("John");
nameList.add("Tony");
nameList.add("Abhay");
nameList.add("Sam");
nameList.add("Danny");
//nameList.sort((s1,s2) -> s1.compareTo(s2));
// Replaced Lambda Expression from above line to Method Reference
nameList.sort(String::compareTo);
nameList.forEach(System.out::println);
}
}
Output:-
Abhay
Danny
John
Sam
Tony
4. Method reference to Constructor
@FunctionalInterface
public interface Welcome{
public abstract void welcome();
}
public class Sample {
//constructor
Sample() {
System.out.println(“Method reference to Constructor”);
}
}
public class MainClass {
public static void main(String[] args) {
// Method Reference to constructor
Welcome welcome2=Sample::new;
welcome2. Welcome();
}
}
Output:
Method reference to Constructor
3. Functional Interfaces
- An interface which has only one abstract method is called Functional Interfaces.
- In short form, it is also called as SAM interface, which means Single Abstract Method Interface.
- Runnable, ActionListener, Comparable are some of the examples of functional interfaces.
Why Functional Interface?
- Functional interface is used for enabling functional programming in the java language.
- It is also used for defining Lambda expressions to a function directly as an argument to a method.
- As we define LE from a FI, we can also define a Functional Interface as a Base type/ Super type of a Lambda Expression.
Syntax:-
interface <interface-name> {
-one abstract method
}
Example:
@FunctionalInterface
interface Multiply
{
int cal(int x);
}
public class Main {
public static void main(String[] args) {
int a = 5;
// lambda expression to define the cal method
Multiply mul = (int x)->x*x;
//parameter passed and return type must be same as defined in the prototype
int result = mul.cal(a);
System.out.println(result);
}
Reference
https://beginnersbook.com/2017/10/java-8-features-with-examples/