Java 8 — Binary Operator | Code Factory

  • It is the child of BiFunction<T,T,T>
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
// andThen, apply inherited from interface java.util.function.BiFunction
static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) { }
static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) { }
}
package com.codeFactory.binaryOperator;import java.util.function.BiFunction;public class Test {
public static void main(String... args) {
BiFunction<String, String, String> f = (s1, s2) -> s1 + s2;
System.out.println(f.apply("Code", "Factory"));
}
}
CodeFactory
package com.codeFactory.binaryOperator;import java.util.function.BinaryOperator;public class Test {
public static void main(String... args) {
BinaryOperator<String> b = (s1, s2) -> s1 + s2;
System.out.println(b.apply("Code", "Factory"));
}
}
CodeFactory

The primitive versions for BinaryOperator :

@FunctionalInterface
public interface IntBinaryOperator {
int applyAsInt(int left, int right);
}
@FunctionalInterface
public interface LongBinaryOperator {
long applyAsLong(long left, long right);
}
@FunctionalInterface
public interface DoubleBinaryOperator {
double applyAsDouble(double left, double right);
}
package com.codeFactory.binaryOperator;import java.util.function.BinaryOperator;public class Test {
public static void main(String... args) {
BinaryOperator<Integer> b = (i1, i2) -> i1 + i2;
System.out.println(b.apply(10, 20));
}
}
30
package com.codeFactory.binaryOperator;import java.util.function.IntBinaryOperator;public class Test {
public static void main(String... args) {
IntBinaryOperator i = (i1, i2) -> i1 + i2;
System.out.println(i.applyAsInt(10, 20));
}
}
30

--

--

--

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

Recommended from Medium

Exploring dependency override in Spring

Understanding WSO2 Stream Processor — Part 2

Simplifying REST APIs

Software Engineers: Avoid Being the Critical Person on Your Team

Working in office

Basics About Python

Starting with GSoC’21 @coala @WeField

Algorithm Series in Javascript — Binary Search

A Serverless Journey

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

Java Theory and Practice

Factory Method pattern in Java

Java Built-In Functional Interfaces

Functional Interfaces in Java