Java 8 UnaryOperator and BinaryOperator

Written by: Arvind Rai,
Last updated:
November 22, 2014
Java 8 has introduced UnaryOperator and BinaryOperator that can be assigned as lambda expression. UnaryOperator and BinaryOperator are functional interface. UnaryOperator extends Function and BinaryOperator extends BiFunction and accordingly they accept argument. UnaryOperator accepts one operand and returns a value of the same type as operand. BinaryOperator accepts two operand of the same type and returns the result of the same type as operand.

UnaryOperator

java.util.function.UnaryOperator is a java 8 functional interface that extends java.util.function.Function. UnaryOperator is used to work on a single operand. It returns the same type as an operand. UnaryOperator can be used as lambda expression to pass as an argument. While defining UnaryOperator, we need to define Function.apply(Object) where Function will be the instance of UnaryOperator. Find the example.
UnaryOperatorDemo.java
package com.concretepage.util.stream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.UnaryOperator;
public class UnaryOperatorDemo {
    public static void main(String[] args) {
       List<Integer> list = Arrays.asList(10,20,30,40,50);
       UnaryOperator<Integer> unaryOpt = i->i*i; 
       unaryOperatorFun(unaryOpt, list).forEach(x->System.out.println(x));       
    }
    private static List<Integer> unaryOperatorFun(UnaryOperator<Integer> unaryOpt, List<Integer> list){
       List<Integer> uniList = new ArrayList<>();
       list.forEach(i->uniList.add(unaryOpt.apply(i))); 
       return uniList;
    }
}
Find the output.
100
400
900
1600
2500 

BinaryOperator

java.util.function.BinaryOperator is a functional interface that can be assigned as lambda expression. BinaryOperator extends java.util.function.BiFunction. It accepts two operands of the same type and process it and then returns results of the same type as operands.
BinaryOperatorDemo.java
package com.concretepage.util.stream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BinaryOperator;
public class BinaryOperatorDemo {
    public static void main(String[] args) {
       Map<String,String> map = new HashMap<>();
       map.put("X", "A");
       map.put("Y", "B");
       map.put("Z", "C");
       BinaryOperator<String> binaryOpt = (s1,s2)-> s1+"-"+s2; 
       binaryOperatorFun(binaryOpt, map).forEach(x->System.out.println(x));       
    }
    private static List<String> binaryOperatorFun(BinaryOperator<String> binaryOpt, Map<String,String> map){
       List<String> biList = new ArrayList<>();
       map.forEach((s1,s2)->biList.add(binaryOpt.apply(s1,s2))); 
       return biList;
    }
} 
Find the output.
X-A
Y-B
Z-C 

BinaryOperator.maxBy and BinaryOperator.minBy

BinaryOperator.maxBy accepts a Compotator and returns BinaryOperator which will return maximum between two elements. In the same way, BinaryOperator.minBy is used to get minimum between two elements.
MaxByMinBy.java
package com.concretepage.util;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
public class MaxByMinBy {
    public static void main(String[] args) {
        Student s1 = new Student("Shyam", 22,"A");
        Student s2 = new Student("Ram",23,"A");
        Student s3 = new Student("Mohan",22,"B");
        Student s4 = new Student("Ramesh",21,"B");
        List<Student> list = Arrays.asList(s1,s2,s3,s4);
        Comparator<Student> ageComparator = Comparator.comparing(Student::getAge); 
        //Using BinaryOperator.maxBy        
        System.out.println("---BinaryOperator.maxBy---");
        Map<String, Optional<Student>> eldestByClass = list.stream().collect(Collectors.groupingBy(Student::getClassName, 
                Collectors.reducing(BinaryOperator.maxBy(ageComparator))));
        eldestByClass.forEach((k,v)->System.out.println("Class:"+k+" Age:"+
                ((Optional<Student>)v).get().getAge()+" Name:"+((Optional<Student>)v).get().getName()));
        
        //Using BinaryOperator.minBy        
        System.out.println("---BinaryOperator.minBy---");
        Map<String, Optional<Student>> youngestByClass = list.stream().collect(Collectors.groupingBy(Student::getClassName, 
                Collectors.reducing(BinaryOperator.minBy(ageComparator))));
        youngestByClass.forEach((k,v)->System.out.println("Class:"+k+" Age:"+
                ((Optional<Student>)v).get().getAge()+" Name:"+((Optional<Student>)v).get().getName()));
    }
} 
Find the Student class used in Example.
Student.java
package com.concretepage.util;
public class Student {
    private String name;
    private Integer age;
    private String className;
    public Student(String name,Integer age, String className){
        this.name=name;
        this.age=age;
        this.className = className;
    }
    public String getName() {
        return name;
    }
    public Integer getAge() {
        return age;
    }
    public String getClassName() {
        return className;
    }
}  
Find the output.
 ---BinaryOperator.maxBy---
Class:A Age:23 Name:Ram
Class:B Age:22 Name:Mohan
---BinaryOperator.minBy---
Class:A Age:22 Name:Shyam
Class:B Age:21 Name:Ramesh 

Join the Newsletter

(Subscribe to get our latest content directly into your inbox)

WRITTEN BY
ARVIND RAI
ARVIND RAI









©2024 concretepage.com | Privacy Policy | Contact Us