GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — BinaryOperator Interface in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

BinaryOperator Interface in Java

Last Updated : 19 Jun, 2026

The BinaryOperator<T> interface is a specialized functional interface introduced in Java 8 and available in the java.util.function package. It represents an operation that takes two operands of the same type and returns a result of the same type.

  • Specialized form of BiFunction<T, T, T>.
  • Accepts two arguments of the same type.
  • Commonly used for comparison, aggregation, and reduction operations.
Java
import java.util.function.BinaryOperator;

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

        BinaryOperator<Integer> add =
            (a, b) -> a + b;

        System.out.println(
            "Sum = " + add.apply(10, 20)
        );
    }
}

Output
Sum = 30

Explanation: In this example, the BinaryOperator accepts two integers and returns their sum. Since the input and output types are the same (Integer), BinaryOperator is a suitable choice.

Syntax

@FunctionalInterface
public interface BinaryOperator<T>
extends BiFunction<T, T, T>

Here,

  • T :Type of both input arguments and the return value.

Methods of BinaryOperator Interface

1. maxBy() Method

The maxBy() method returns a BinaryOperator that selects the greater of two elements according to the specified Comparator. Returns the maximum of two values.

  • Uses a Comparator for comparison.
  • Useful for finding the largest element.

Syntax:

static <T> BinaryOperator<T>
maxBy(Comparator<? super T> comparator)

  • Parameters: comparator: Comparator used to compare two values.
  • Return Value: Returns a BinaryOperator that returns the larger of the two operands.
Java
import java.util.function.BinaryOperator;

public class GFG {
    public static void main(String args[])
    {
        BinaryOperator<Integer>
            op = BinaryOperator
                     .maxBy(
                         (a, b) -> (a > b) ? 1 : ((a == b) ? 0 : -1));

        System.out.println(op.apply(98, 11));
    }
}

Output
98

Explanation: The maxBy() method compares 98 and 11 using Integer.compare(). Since 98 is greater, it is returned as the result.

2. minBy()

This method returns a BinaryOperator which returns the lesser of the two elements based on a given comparator

Syntax: 

static <T> BinaryOperator<T> 
minBy(Comparator<? super T> comparator)

Parameters: It takes in only one parameter namely, comparator which is an object of the Comparator class.
Returns: This method returns a BinaryOperator which return the minimum of the two objects passed while calling it based on the given comparator.

Java
import java.util.function.BinaryOperator;

public class GFG {
    public static void main(String args[])
    {
        BinaryOperator<Integer>
            op = BinaryOperator
                     .minBy(
                         (a, b) -> (a > b) ? 1 : ((a == b) ? 0 : -1));

        System.out.println(op.apply(98, 11));
    }
}

Output
11

Explanation: The minBy() method compares 98 and 11 using Integer.compare(). Since 11 is smaller, it is returned as the result.

BinaryOperator vs BiFunction

FeatureBinaryOperatorBiFunction
Input ArgumentsSame TypeCan Be Different Types
Return TypeSame as Input TypeCan Be Different
ExtendsBiFunction<T,T,T>Functional Interface
Use CaseComparison, ReductionGeneral Two-Input Operations
Packagejava.util.functionjava.util.function

Advantages of BinaryOperator

  • Simplifies operations where all types are the same.
  • Reduces boilerplate code compared to BiFunction.
  • Supports lambda expressions and method references.
  • Useful for finding minimum and maximum values.
  • Commonly used in Stream API reduction operations.
  • Improves code readability and maintainability.
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.