|
|
How to return lambda as an object?
A lambda expression is an instance of a functional interface, which is itself a subtype of Object.
This example shows how to retrieve lambda as an object.
package com.java2novice.lambda;
public class Employee {
private String name;
private String account;
private Integer salary;
public Employee(String name, String account, Integer salary) {
super();
this.name = name;
this.account = account;
this.salary = salary;
}
@Override
public String toString() {
return "name: "+ this.name +" | account: "+ this.account +" | salary: "+this.salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
}
|
package com.java2novice.lambda;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class LambdaAsObjectEx {
public static void main(String a[]) {
List<Employee> empList = new ArrayList<>();
empList.add(new Employee("Nataraja G", "Accounts", 8000));
empList.add(new Employee("Nagesh Y", "Admin", 15000));
empList.add(new Employee("Vasu V", "Security", 2500));
empList.add(new Employee("Amar", "Entertainment", 8500));
LambdaAsObjectEx.sortInLambdaWay(empList);
}
public static void sortInLambdaWay(List<Employee> empList) {
Comparator<Employee> cmpDesc = (Employee e1, Employee e2) -> e2.getSalary().compareTo(e1.getSalary());
Collections.sort(empList, cmpDesc);
System.out.println("<--- Sorted list with Lambda - DESC order --->");
empList.forEach(emp->System.out.println(emp.toString()));
Comparator<Employee> cmpAsc = (Employee e1, Employee e2) -> e1.getSalary().compareTo(e2.getSalary());
Collections.sort(empList, cmpAsc);
System.out.println("\n\n<--- Sorted list with Lambda - ASC order --->");
empList.forEach(emp->System.out.println(emp.toString()));
}
}
|
|
Output: |
<--- Sorted list with Lambda - DESC order --->
name: Nagesh Y | account: Admin | salary: 15000
name: Amar | account: Entertainment | salary: 8500
name: Nataraja G | account: Accounts | salary: 8000
name: Vasu V | account: Security | salary: 2500
<--- Sorted list with Lambda - ASC order --->
name: Vasu V | account: Security | salary: 2500
name: Nataraja G | account: Accounts | salary: 8000
name: Amar | account: Entertainment | salary: 8500
name: Nagesh Y | account: Admin | salary: 15000
|
|
|
|
|
Java-8 Lambda expression examples
- Lambda expression syntax example
- Lambda example with single method interface implementation.
- Comparator example with Lambda implementation.
- How to return lambda as an object?
|
|
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based.
But accessing is difficult with LinkedList. It is slow access. This is
to access any element, you need to navigate through the elements one by
one. But insertion and deletion is much faster with LinkedList, because
if you know the node, just change the pointers before or after nodes.
Insertion and deletion is slow with ArrayList, this is because, during
these operations ArrayList need to adjust the indexes according to
deletion or insetion if you are performing on middle indexes. Means,
an ArrayList having 10 elements, if you are inserting at index 5, then
you need to shift the indexes above 5 to one more.
The very best thing you can do for the whole world is to make the most of yourself.
-- Wallace Wattles
|