Java Lambda Expression
Example: Comparator
package
govind;
public class
Employee {
int empid;
String empName;
String empsalary;
public
Employee(int empid, String empName, String empsalary) {
super();
this.empid = empid;
this.empName = empName;
this.empsalary = empsalary;
}}
package govind;
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.List;
public
class EmpMain {
public
static void
main(String[] args) {
List<Employee> l=new
ArrayList<>();
Employee e1=new Employee(101, "govind",
"11000");
l.add(e1);
Employee e2=new Employee(102, "sanjeet",
"21000");
l.add(e2);
Employee e3=new Employee(103, "ajit",
"28000");
l.add(e3);
Employee e4=new Employee(104, "mani",
"45000");
l.add(e4);
System.out.println("sorting on the basis of name");
//
implementing lambda expression
Collections.sort(l, (ep1,ep2)->{
return ep1.empName.compareTo(ep2.empName);
});
for(Employee e:l)
{
System.out.println(e.empName+" "+e.empsalary+" "+e.empid);
}}}
o/p:----------------
sorting on the basis of name
ajit 28000 103
govind 11000 101
mani 45000 104
sanjeet 21000 102
No comments:
Post a Comment