Java Lambda Expressions
Lambda expressions are introduced in Java 8 and are touted to be the most immensely colossal feature of Java. A lambda expression is an innominate function. A function that doesn’t have a designation and doesn’t belong to any class.
It preserves an abundance of code and provides the implementation of a Functional interface.
Lambda expressions are homogeneous to methods, but they do not require a denomination and they can be implemented right in the body of a method.
//Syntax of lambda expression
(parameter_list) -> {function_body}
Java lambda expression has consisted of three components.
1) Parameter_list: It can be vacuous or non-empty as well.
2) Arrow-token: It is utilized to link arguments-list and body of expression.
3) Function_body: It contains expressions and verbalizations for a lambda expression.
Lambda Expression Example: Single Parameter
interface Fruit{
public String setFruit(String fruit);
}
class LambdaExpressionExample {
public static void main(String[] args) {
// Lambda expression with single parameter.
Fruit s1=(fruit)->{
return "Apple, "+fruit;
};
System.out.println(s1.setFruit("Fruit"));
// You can omit function parentheses
Fruit s2= fruit ->{
return "Apple, "+fruit;
};
System.out.println(s2.setFruit("Fruit"));
}
}
Output
Apple, Fruit
Apple, Fruit
Lambda Expression Example: Multiple Parameters
interface Fruit{
public String setFruit(String fruit, double price);
}
class LambdaExpressionExample {
public static void main(String[] args) {
// Lambda expression with single parameter.
Fruit s1=(fruit,price)->{
return "FRUIT, "+fruit+","+price+"RS";
};
System.out.println(s1.setFruit("Fruit",44.44));
}
}
Output
Fruit, Fruit,44.44RS
Lambda Expression Example: No Parameter
interface Fruit{
public String fruit();
}
class LambdaExpressionExample {
public static void main(String[] args) {
// Lambda expression with single parameter.
Fruit s1=()->{
return "Apple";
};
System.out.println(s1.fruit());
}
}
Output
Apple
Lambda Expression Example: Foreach Loop
import java.util.*;
class LambdaExpressionExample {
public static void main(String[] args) {
Set<String> list=new HashSet<>();
list.add("Java");
list.add("Kotlin");
list.add("Groovy");
list.add("Scala");
list.forEach(
(language)->System.out.println(language)
);
}
}
Output
Java
Scala
Groovy
Kotlin
Lambda Expression Example: Comparator
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class User {
int id;
String name;
int age;
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
class LambdaExpressionExample {
public static void main(String[] args) {
List<User> list = new ArrayList<User>();
// Adding employees
list.add(new User(115, "Sibin", 54));
list.add(new User(125, "Sabin", 45));
list.add(new User(135, "Arun", 44));
System.out.println("Sorting the user list based on the name");
// implementing lambda expression
Collections.sort(list, (p1, p2) -> {
return p1.name.compareTo(p2.name);
});
for(User e : list) {
System.out.println(e.id + " " + e.name + " " + e.age);
}
}
}
Output
Sorting the user list based on the name
135 Arun 44
125 Sabin 45
115 Sibin 54
Lambda Expression Example: Iterating Map
import java.util.HashMap;
import java.util.Map;
class LambdaExpressionExample {
public static void main(String[] args) {
Map<String, Long> prices = new HashMap<>();
prices.put("Java", 1323L);
prices.put("Python", 1234L);
prices.put("C", 999L);
prices.put("C++", 987L);
prices.put("Go", 889L);
prices.forEach((k,v)->System.out.println("Skill: " + k + ", Salary: " + v));
}
}
Output
Skill: Java, Salary: 1323
Skill: C++, Salary: 987
Skill: C, Salary: 999
Skill: Go, Salary: 889
Skill: Python, Salary: 1234