Java 8 — Binary Operator | Code Factory

@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

--

--

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