Wednesday 13 May 2020

Lambda Expression in JAVA



-> The Lambda expression is used to provide the implementation of an interface which has functional interface.

->  It saves a lot of code.

-> In case of lambda expression, we don't need to define the method again for providing the implementation. Here, we just write the implementation code.

-> Java lambda expression is treated as a function, so compiler does not create .class file.



Functional Interface

-> Lambda expression provides implementation of functional interface.

-> An interface which has only one abstract method is called functional interface.

-> Java provides an anotation @FunctionalInterface, which is used to declare an interface as functional interface.


Syntax

(argument-list) -> {body}


Java lambda expression is consisted of three components.

1) Argument-list: It can be empty or non-empty as well.

2) Arrow-token: It is used to link arguments-list and body of expression.

3) Body: It contains expressions and statements for lambda expression.



Example :  Addition of two numbers using lamda expression

@FunctionalInterface
interface Calc{

   int add(int n1,int n2);

}




public class Calculation{
 
 
    public static void main(String[] args) { 

     
       Calc addition=(n1,n2)->{

          return n1+n2;
       };
   
 
       System.out.println("Addition "+addition.add(10,20)); 


       
 }


}


Output

Addition 30

Comparator Interface in java


-> Comparator used to sorting the object element by using multiple sorting sequence.

-> We can sort the elements on the basis of any data member, for example, rollno, name, age or anything else.

-> The comparator interface provide two methods like compare(Object obj1,Object obj2) and equals(Object obj).



1) compare(Object obj1,Object obj2) function

Its compares first object(obj1) and second object(obj2) and return int value

Syntax : public int compare(Object obj1,Object obj2)


positive integer = if the current object is greater than the specified object.
negative integer = if the current object is less than the specified object.
zero = if the current object is equal to the specified object.



2) equals(Object obj1) function

Its used to compare current object with specified object

Syntax : public boolean equals(Object obj1)




Example : Sorting by name and Emp id by using comparator

import java.util.*; 
import java.io.*;

// User - Defined Class

class Employee { 
int empid; 
String empname; 
int empage; 
Employee(int empid,String empname,int empage){ 
this.empid=empid; 
this.empname=empname; 
this.empage=empage; 

}


class sortByAge implements Comparator{
                         
public int compare(Object o1,Object o2){ 
Employee s1=(Employee)o1; 
Employee s2=(Employee)o2; 

if(s1.empage==s2.empage) 
return 0; 
else if(s1.empage>s2.empage) 
return 1; 
else 
return -1; 

   
}
 

class sortByName implements Comperator{
   
public int compare(Object o1,Object o2){ 
Employee s1=(Employee)o1; 
Employee s2=(Employee)o2; 

return s1.empname.compareTo(s2.empname);
   
}   
}


// Main Class



public class TestSort1{ 
public static void main(String args[]){ 
ArrayList<Employee> al=new ArrayList<Employee>(); 
al.add(new Employee(1,"Ajith",23)); 
al.add(new Employee(6,"Vijay",27)); 
al.add(new Employee(4,"Surya",21)); 
 
 
System.out.println("Sorting By Age"); 
Collections.sort(al, new sortByAge()); 
for(Employee st:al){ 
System.out.println(st.empid+" "+st.empname+" "+st.empage); 
}


System.out.println("Sorting By Name");
Collections.sort(al, new sortByName()); 
for(Employee st:al){ 
System.out.println(st.empid+" "+st.empname+" "+st.empage); 






Output

Sorting By Age

4 Surya 21
1 Ajith 23
6 Vijay 27


Sorting By Name

1 Ajith 23
4 Surya 21
6 Vijay 27

Comparable interface in java



-> Comparable is an interface of comparing the objects with other objects os the same type. This is also called "natural ordering"

-> by using compare interface, we can sort the elements based on single data member only.

-> It provides single method name as compareTo(object) and provides single sorting sequence only.

-> The interface found in java.lang.package


compareTo(Object obj) method

The method used to compare the current object with specified object. Please find the below return values

positive integer = if the current object is greater than the specified object.
negative integer = if the current object is less than the specified object.
zero = if the current object is equal to the specified object.



Example 1: Sort by Integer Value (Ascending Order) by using Comparable

import java.util.*; 

// User - Defined Class

class Employee implements Comparable<Employee>{ 
int empid; 
String empname; 
int empage; 
Employee(int empid,String empname,int empage){ 
this.empid=empid; 
this.empname=empname; 
this.empage=empage; 

 
public int compareTo(Employee st){ 
if(empage==st.empage) 
return 0; 
else if(empage>st.empage) 
return 1; 
else 
return -1; 

}



// Main Class



public class TestSort1{ 
public static void main(String args[]){ 
ArrayList<Employee> al=new ArrayList<Employee>(); 
al.add(new Employee(1,"Ajith",23)); 
al.add(new Employee(6,"Vijay",27)); 
al.add(new Employee(4,"Surya",21)); 
 
Collections.sort(al); 
for(Employee st:al){ 
System.out.println(st.empid+" "+st.empname+" "+st.empage); 







Output

4 Surya 21
1 Ajith 23
6 Vijay 27


********************************************************


Example 2: Sort by Integer Value (Decending Order) by using Comparable


import java.util.*; 

// User - Defined Class

class Employee implements Comparable<Employee>{ 
int empid; 
String empname; 
int empage; 
Employee(int empid,String empname,int empage){ 
this.empid=empid; 
this.empname=empname; 
this.empage=empage; 

 
public int compareTo(Employee st){ 
if(empage==st.empage) 
return 0; 
else if(empage<st.empage) 
return 1; 
else 
return -1; 

}



// Main Class



public class TestSort1{ 
public static void main(String args[]){ 
ArrayList<Employee> al=new ArrayList<Employee>(); 
al.add(new Employee(1,"Ajith",23)); 
al.add(new Employee(6,"Vijay",27)); 
al.add(new Employee(4,"Surya",21)); 
 
Collections.sort(al); 
for(Employee st:al){ 
System.out.println(st.empid+" "+st.empname+" "+st.empage); 







Output


6 Vijay 27
1 Ajith 23
4 Surya 21