Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
BinaryOperator Interface in Java
Next article icon

BinaryOperator Interface in Java

Last Updated : 30 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The BinaryOperator Interface<T> is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a binary operator which takes two operands and operates on them to produce a result. However, what distinguishes it from a normal BiFunciton is that both of its arguments and its return type are same.

This functional interface which takes in one generic namely:- 

  • T: denotes the type of the input arguments and the return value of the operation

The BinaryOperator<T> extends the BiFunction<T, T, T> type. So it inherits the following methods from the BiFunction Interface:  

  • apply(T t, T u)
  • andThen(Function<? super R, ? extends V> after)

The lambda expression assigned to an object of BiaryOperator type is used to define its apply() which eventually applies the given operation on its arguments.  

Functions in BinaryOperator Interface

The BinaryOperator interface consists of the following functions: 

1. maxBy()

This methods return a BinaryOperator which returns the greater of the two elements based on a given comparator

Syntax:  

static <T> BinaryOperator<T>      maxBy(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 returns the maximum of the two objects passed while calling it based on the given comparator.

Below is the code to illustrate maxBy() method:

Program:  

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

 

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.

Below is the code to illustrate minBy() method:

Program: 

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

 

Next Article
BinaryOperator Interface in Java

P

psil123
Improve
Article Tags :
  • Java
  • Java - util package
  • java-basics
  • Java 8
Practice Tags :
  • Java

Similar Reads

    Collection Interface in Java
    The Collection interface in Java is a core member of the Java Collections Framework located in the java.util package. It is one of the root interfaces of the Java Collection Hierarchy. The Collection interface is not directly implemented by any class. Instead, it is implemented indirectly through it
    6 min read
    Function Interface in Java
    The Function Interface is a part of the java.util.function package that has been introduced since Java 8, to implement functional programming in Java. It represents a function that takes in one argument and produces a result. Hence, this functional interface takes in 2 generics, namely as follows:T:
    6 min read
    Cloneable Interface in Java
    The Java.lang.Cloneable interface is a marker interface. It was introduced in JDK 1.0. There is a method clone() in the Object class. Cloneable interface is implemented by a class to make Object.clone() method valid thereby making field-for-field copy. This interface allows the implementing class to
    4 min read
    Evolution of Interface in Java
    In Java, interfaces define a contract for classes without specifying how the methods are implemented. With time, Java has introduced major enhancements or we can say evolution of interfaces to make them more powerful. So, in this article, we will explore the evolution of interfaces in Java from Java
    3 min read
    Java.util.function.DoubleBinaryOperator interface with Examples
    The DoubleBinaryOperator interface was introduced in Java 8. It represents an operation on two double values and returns the result as a double value. It is a functional interface and thus can be used as a lambda expression or in a method reference. It is mostly used when the operation needs to be e
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences