java 8 - Predefined Functional Interface - Function Examples

Eg: Remove spaces in the string

class Test
{
public static void main(String[] args)
{
String s="CTS is one of the MNC";
Function<String,String> f= s1->s1.replaceAll(" ","");
System.out.println(f.apply(s));
}
}

Eg: Number of spaces present is the given string
class Test
{
public static void main(String[] args)
{
String s="durga software solutions hyderabad ";
Function<String,Integer> f= s1->s1.length() - s1.replaceAll(" ","").length();
System.out.println(f.apply(s));
}
 }

Write Program to find total Monthly salary of all employees using function:
class Employee
{
String name;
double salary;
Employee(String name,double salary)
{
this.name=name;
this.salary=salary;
}
public String toString()
{
return name+":"+salary;
}
}


class Test
{
public static void main(String[] args)
{
ArrayList<Employee> l= new ArrayList<Employee>();
populate(l);
System.out.println(l);

Function<ArrayList<Employee>,Double> f= l1 ->{
double total=0;
for(Employee e: l1)
{
total=total+e.salary;
}
return total;
};

System.out.println("The total salary of this month:"+f.apply(l));
}

public static void populate(ArrayList<Employee> l)
{
l.add(new Employee("Sunny",1000));

l.add(new Employee("Bunny",2000));
l.add(new Employee("Chinny",3000));
l.add(new Employee("Pinny",4000));
l.add(new Employee("Vinny",5000));
}
}

Output:
[Sunny:1000.0, Bunny:2000.0, Chinny:3000.0, Pinny:4000.0, Vinny:5000.0]
The total salary of this month:15000.0

Function Chaining:
* If you want to combine two functions then we will go for function chaining.
* andThen(), compose() - default method is helpful for us to do this.
f1.andThen(f2) - First f1 will apply then f2 will apply.
f1.compose(f2) - First f2 will apply then f1 will apply.

Eg Program:
class Test
{
public static void main(String[] args)
{
Function<String,String> f1=s->s.toUpperCase();
Function<String,String> f2= s->s.substring(0,9);

System.out.println("The Result of f1:"+f1.apply("AishwaryaAbhi"));
System.out.println("The Result of f2:"+f2.apply("AishwaryaAbhi"));
System.out.println("The Result of f1.andThen(f2):"+f1.andThen(f2).apply("AishwaryaAbhi"));
System.out.println("The Result of f1.compose(f2):"+f1.compose(f2).apply("AishwaryaAbhi"));
}
}

Output:
The Result of f1:AISHWARYAABHI
The Result of f2:Aishwarya
The Result of f1.andThen(f2):AISHWARYA
The Result of f1.compose(f2):AISHWARYA

Difference between andThen and Compose
class Test
{
public static void main(String[] args)
{
Function<Integer,Integer> f1= i->i+i;
Function<Integer,Integer> f2= i->i*i*i;
System.out.println(f1.andThen(f2).apply(2));
System.out.println(f1.compose(f2).apply(2));
}
}

Output:
64
16


Function Interface Static Methods:
* static <T> Function<T,T> identity()
* Whatever input you are giving return the same input back.
* No use of this method.
Eg:
class Test
{
public static void main(String[] args)
{
Function<String,String> f1= Function.identity();
String s2= f1.apply("vinoth");
System.out.println(s2);
}
}
output: vinoth

Comments

Popular posts from this blog

How to set Java Object into JaxBElement ?

GitLab