Java 8 — Function | Code Factory

  • Functions are exactly same as predicates except that functions can return any type of result but function should (can) return only one value and that value can be any type as per our requirement.
  • To implement functions oracle people introduced Function interface in 1.8v.
  • Function interface present in Java.util.function package.
  • Functional interface contains only one method i.e., apply()
@FunctionalInterface
public interface Function<T,R> {
R apply(T t);
default <V> Function<V,R> compose(Function<? super V,? extends T> before) { }
default <V> Function<T,V> andThen(Function<? super R,? extends V> after) { }
static <T> Function<T,T> identity() { }
}

Write a function to find length of given input string :

package com.codeFactory.function;import java.util.function.Function;public class Test {
public static void main(String... args) {
Function<String, Integer> f = s -> s.length();
System.out.println(f.apply("Code"));
System.out.println(f.apply("Factory"));
}
}
4
7

Differences between predicate and function :

Program to remove spaces present in the given String by using Function :

package com.codeFactory.function;import java.util.function.Function;public class Test {
public static void main(String... args) {
String s = "Code Factory . .";
Function<String, String> f = s1 -> s1.replace(" ", "");
System.out.println(f.apply(s));
}
}
CodeFactory..

Program to find Number of spaces present in the given String by using Function :

package com.codeFactory.function;import java.util.function.Function;public class Test {
public static void main(String... args) {
String s = "Code Factory . .";
Function<String, Integer> f = s1 -> s1.length() - s1.replace(" ", "").length();
System.out.println(f.apply(s));
}
}
6

Program to find Student Grade by using Function :

package com.codeFactory.function;import java.util.ArrayList;
import java.util.function.Function;
class Student {
String name;
int marks;
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}

@Override
public String toString() {
return "Student [name=" + name + ", marks=" + marks + "]";
}
}
public class Test {
public static void main(String... args) {
ArrayList<Student> i = new ArrayList<Student>();
populate(i);
Function<Student, String> f = s -> {
int marks = s.marks;
if(marks >= 80) {
return "A[Distinction]";
} else if(marks >= 60) {
return "B[First class]";
} else if(marks >= 50) {
return "C[Second class]";
} else if(marks >= 35) {
return "D[Third class]";
} else {
return "E[Failed]";
}
};
for(Student s : i) {
System.out.println(s + " -> " + f.apply(s));
}
}

public static void populate(ArrayList<Student> i) {
i.add(new Student("Narendra", 100));
i.add(new Student("Amit", 65));
i.add(new Student("Vijay", 55));
i.add(new Student("Nitin", 45));
i.add(new Student("Nahera", 25));
}
}
Student [name=Narendra, marks=100] -> A[Distinction]
Student [name=Amit, marks=65] -> B[First class]
Student [name=Vijay, marks=55] -> C[Second class]
Student [name=Nitin, marks=45] -> D[Third class]
Student [name=Nahera, marks=25] -> E[Failed]

Progarm to perform Salary Increment for Employees by using Predicate & Function :

package com.codeFactory.function;import java.util.ArrayList;
import java.util.function.Function;
import java.util.function.Predicate;
class Employee {
String name;
double salary;
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}

@Override
public String toString() {
return "{name=" + name + ", salary=" + salary + "}";
}
}
public class Test {
public static void main(String... args) {
ArrayList<Employee> i = new ArrayList<Employee>();
populate(i);

System.out.println("Before Increment :");
System.out.println(i);

Predicate<Employee> p = e -> e.salary < 4000;
Function<Employee, Employee> f = e -> {
e.salary += 500;
return e;
};

ArrayList<Employee> i2 = new ArrayList<Employee>();
for(Employee e : i) {
if(p.test(e)) {
i2.add(f.apply(e));
}
}

System.out.println("After Increment :");
System.out.println(i);

System.out.println("Employees with incremented salary :");
System.out.println(i2);
}

public static void populate(ArrayList<Employee> i) {
i.add(new Employee("Narendra", 1000));
i.add(new Employee("Amit", 2000));
i.add(new Employee("Vijay", 3000));
i.add(new Employee("Nitin", 4000));
i.add(new Employee("Nahera", 5000));
i.add(new Employee("Yogi", 10000));
}
}
Before Increment :
[{name=Narendra, salary=1000.0}, {name=Amit, salary=2000.0}, {name=Vijay, salary=3000.0}, {name=Nitin, salary=4000.0}, {name=Nahera, salary=5000.0}, {name=Yogi, salary=10000.0}]
After Increment :
[{name=Narendra, salary=1500.0}, {name=Amit, salary=2500.0}, {name=Vijay, salary=3500.0}, {name=Nitin, salary=4000.0}, {name=Nahera, salary=5000.0}, {name=Yogi, salary=10000.0}]
Employees with incremented salary :
[{name=Narendra, salary=1500.0}, {name=Amit, salary=2500.0}, {name=Vijay, salary=3500.0}]

Function Chaining :

  • f1.andThen(f2) : First f1 will be applied and then for the result f2 will be applied
  • f1.compose(f2) : First f2 will be applied and then for the result f1 will be applied
package com.codeFactory.function;import java.util.function.Function;public class Test {
public static void main(String... args) {
Function<String, String> f1 = s -> s.toUpperCase();
Function<String, String> f2 = s -> s.substring(0, 4);

System.out.println("f1 : " + f1.apply("Code"));
System.out.println("f2 : " + f2.apply("Factory"));
System.out.println("f1.andThen(f2) : " + f1.andThen(f2).apply("Code Factory"));
System.out.println("f1.compose(f2) : " + f1.compose(f2).apply("Code Factory"));
}
}
f1 : CODE
f2 : Fact
f1.andThen(f2) : CODE
f1.compose(f2) : CODE

Program to Demonstrate the difference between andThen() and compose() :

package com.codeFactory.function;import java.util.function.Function;public 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 : " + f1.apply(2));
System.out.println("f2 : " + f2.apply(3));
System.out.println("f1.andThen(f2) : " + f1.andThen(f2).apply(4));
System.out.println("f1.compose(f2) : " + f1.compose(f2).apply(4));
}
}
f1 : 4
f2 : 27
f1.andThen(f2) : 512
f1.compose(f2) : 128

Demo Program for User Authentication by using Function Chaining :

package com.codeFactory.function;import java.util.Scanner;
import java.util.function.Function;
public class Test {
public static void main(String... args) {
Function<String, String> f1 = s -> s.toLowerCase();
Function<String, String> f2 = s -> s.substring(0, 4);

Scanner sc = new Scanner(System.in);
System.out.println("Enter user name :");
String userName = sc.next();

System.out.println("Enter password :");
String psw = sc.next();

if(f1.andThen(f2).apply(userName).equals("code") && psw.equals("code")) {
System.out.println("Welcome Code Factory...");
} else {
System.out.println("Sorry...");
}

}
}
Enter user name :
CODE
Enter password :
code
Welcome Code Factory...
Enter user name :
Code132
Enter password :
code
Welcome Code Factory...
Enter user name :
132code
Enter password :
code
Sorry...

Function interface Static Method : identity()

package com.codeFactory.function;import java.util.function.Function;public class Test {
public static void main(String... args) {
Function<String, String> f1 = Function.identity();
String s = f1.apply("Code Factory");
System.out.println(s);
}
}
Code Factory

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Divide and Conquer

Weekly Update| Ares Protocol airdrop activity worth 50,000 USDT is raging on

Spark 3 Reduced Our EMR Cost by 40%

Realtime ChatBot App Development

Amazon VPC — Subnets, Route Tables, Internet Gateway

Should I learn Python or Java in 2022 ?

Boosting Python with Cython and Numba

has_many :selves?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Singleton Design Pattern in Java

Java Post — Immutable Strings

Java vs C sharp: Top 8 Important Differences To Know

Comparison Inheritance And Polymorphism in JAVA