Java Functional Interfaces – UnaryOperator & BinaryOperator
This section describes the functional interfaces UnaryOperator and BinaryOperator defined in java.util.Function package in Java 8.
What is it?
public interface UnaryOperator<T> extends Function<T,T>
There are three primitive unary operators for Integer, Long and Double – IntUnaryOperator, LongUnaryOperator and DoubleUnaryOperator – that accept corresponding value types and return same value types.
public interface BinaryOperator<T> extends BiFunction<T,T,T>
There are three primitive unary operators for Integer, Long and Double – IntBinaryOperator, LongBinaryOperator and DoubleBinaryOperator – that accept corresponding value types and return same value types.
The BinaryOperator interface consists of following functions:
Why it was introduced?
Examples
The examples illustrated below use the following Employee POJO class:
package org.examples;
public class Employee { private String name; private Integer hourRate; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getHourRate() { return hourRate; } public void setHourRate(Integer hourRate) { this.hourRate = hourRate; } public Employee(String name, Integer hourRate) { super(); this.name = name; this.hourRate = hourRate; }
} |
package org.examples;
import java.util.ArrayList; import java.util.List; import java.util.function.IntUnaryOperator;
public class UnaryExample1 { public static void main(String[] args) {
List<Employee> employees = Arrays.asList(new Employee(“Jack”, 10), new Employee(“Robert”, 50), new Employee(“David”, 30), new Employee(“Abhi”, 20));
employees.forEach(emp -> System.out.println(getDailyRateFunc().applyAsInt(emp.getHourRate()))); }
public static IntUnaryOperator getDailyRateFunc() { return hourRate -> hourRate * 8; } } |
Output: 80 400 240 160 |
package org.examples;
import java.util.ArrayList; import java.util.List; import java.util.function.UnaryOperator;
public class UnaryExample2 { public static void main(String[] args) { List<Employee> employees = Arrays.asList(new Employee(“Jack”, 10), new Employee(“Robert”, 50), new Employee(“David”, 30), new Employee(“Abhi”, 20));
List<Employee> newRateEmployees = changeRate(employees, getIncrementRateFunction()); newRateEmployees.forEach(employee -> System.out.println(employee.getHourRate())); }
public static UnaryOperator<Employee> getIncrementRateFunction() { UnaryOperator<Employee> incrementRate = emp -> { emp.setHourRate(emp.getHourRate()*2); return emp; }; return incrementRate; }
public static List<Employee> changeRate(List<Employee> employeeList, UnaryOperator<Employee> unaryFunc) { List<Employee> newRateEmployees = new ArrayList<>(); employeeList.forEach(emp -> newRateEmployees.add(unaryFunc.apply(emp))); return newRateEmployees; } } |
Output: 20 100 60 40 |
package org.examples;
import java.util.ArrayList; import java.util.List; import java.util.function.UnaryOperator;
public class UnaryExample3 { public static void main(String[] args) { List<Employee> employees = Arrays.asList(new Employee(“Jack”, 10), new Employee(“Robert”, 50), new Employee(“David”, 30), new Employee(“Abhi”, 20));
List<Employee> newRateEmployees = modifyName(employees,getChangeCaseFunction(),getAddSalutationFunction()); newRateEmployees.forEach(employee -> System.out.println(employee.getName()));
}
public static UnaryOperator<Employee> getChangeCaseFunction() { UnaryOperator<Employee> changeCase = emp -> { emp.setName(emp.getName().toUpperCase()); return emp; }; return changeCase; }
public static UnaryOperator<Employee> getAddSalutationFunction() { UnaryOperator<Employee> addSalutation = emp -> { emp.setName(“MR. “ + emp.getName()); return emp; }; return addSalutation; }
public static List<Employee> modifyName(List<Employee> employeeList, UnaryOperator<Employee> unaryFunc1, UnaryOperator<Employee> unaryFunc2) { List<Employee> newEmployeesList = new ArrayList<>(); employeeList.forEach(emp -> newEmployeesList.add(unaryFunc1.andThen(unaryFunc2).apply(emp))); return newEmployeesList; } } |
Output: MR. JACK MR. ROBERT MR. DAVID MR. ABHI |
package org.examples;
import java.util.ArrayList; import java.util.List; import java.util.function.BinaryOperator;
public class UnaryExample1 { public static void main(String[] args) { List<Employee> employees = Arrays.asList(new Employee(“Jack”, 10), new Employee(“Robert”, 50), new Employee(“David”, 30), new Employee(“Abhi”, 20));
Integer result = getTotalRate(employees, getTotalRateFunction()); System.out.println(result); }
public static BinaryOperator<Integer> getTotalRateFunction() { return (rate1,rate2) -> rate1 + rate2; }
public static Integer getTotalRate(List<Employee> employees, BinaryOperator<Integer> binaryFunc) { Integer total = 0; for(Employee emp : employees) { total = binaryFunc.apply(total, emp.getHourRate()); } return total; } } |
Output: 110 |
package org.examples;
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.function.BinaryOperator;
public class BinaryExample3 { public static void main(String[] args) { List<Employee> employees = Arrays.asList(new Employee(“Jack”, 10), new Employee(“Robert”, 50), new Employee(“David”, 30), new Employee(“Abhi”, 20));
BinaryOperator<Employee> maxOp = BinaryOperator.maxBy(compareRate);
Employee maxRateEmp = findEmployee(employees, maxOp); System.out.println(“Max rate: “ + maxRateEmp.getHourRate());
BinaryOperator<Employee> minOp = BinaryOperator.minBy(compareRate);
Employee minRateEmp = findEmployee(employees, minOp); System.out.println(“Min rate: “ + minRateEmp.getHourRate()); }
public Comparator<Employee> getRateComparator() { Comparator<Employee> compareRate = new Comparator<Employee>() { @Override public int compare(Employee o1, Employee o2) { return o1.getHourRate().compareTo(o2.getHourRate()); } }; return compareRate; }
public static Employee findEmployee(List<Employee> employees, BinaryOperator<Employee> binaryFunc) { Employee resultEmp = null; for(Employee emp : employees) { if(resultEmp == null) resultEmp = emp; else resultEmp = binaryFunc.apply(resultEmp, emp); } return resultEmp; } } |
Output: Max rate: 50 Min rate: 10 |
Leave a Reply