Java Reference ยท Streams and Functional
UnaryOperator and BinaryOperator interfaces
Reference for java.util.function.UnaryOperator and BinaryOperator: same-type functions, identity(), minBy(), maxBy(), and use in replaceAll, reduce and merge.
UnaryOperator<T> is a Function<T, T>: it takes a value and returns a value of the same type. BinaryOperator<T> is a BiFunction<T, T, T>: it takes two values of one type and returns one of that type. They add no new abstract method; the method is still apply(). Their purpose is to say "same type in and out" in the signature, and to remove repeated type parameters.
You see them in List.replaceAll() (unary), Stream.iterate() (unary), Stream.reduce() (binary), Map.merge() (a BiFunction, but a BinaryOperator fits), and Collectors.toMap()'s merge function.
UnaryOperator<String> trim = String::strip; BinaryOperator<Integer> max = Math::max;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
@FunctionalInterface public interface UnaryOperator<T> extends Function<T, T> { static <T> UnaryOperator<T> identity(); } @FunctionalInterface public interface BinaryOperator<T> extends BiFunction<T, T, T> { static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator); static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator); }
| Member | Description |
|---|---|
apply(t) / apply(t1, t2) |
Inherited abstract method; computes the result. |
UnaryOperator.identity() |
Operator returning its input unchanged. |
BinaryOperator.minBy(c) |
Returns the lesser of two values by the comparator; the first argument on a tie. |
BinaryOperator.maxBy(c) |
Returns the greater of two values by the comparator; the first argument on a tie. |
Primitive forms: IntUnaryOperator, IntBinaryOperator, and the Long and Double versions, plus DoubleBinaryOperator and friends.
Usage notes
- A
UnaryOperatoris aFunction, soandThenandcomposeare inherited, but they return a plainFunction, not aUnaryOperator. reduce()needs associativity.Integer::sum,Math::maxand string concatenation are fine; subtraction is not, especially in parallel streams.- Merge functions passed to
Collectors.toMap()resolve duplicate keys; without one, a duplicate key throwsIllegalStateException. - Use primitive operators such as
IntBinaryOperatorwithIntStream.reduce()to avoid boxing. - Type inference usually allows
(a, b) -> a + bforBinaryOperator<Integer>, but not mixed types: that needs aBiFunction. replaceAllapplies the operator to every element and replaces it; on an immutable list it throwsUnsupportedOperationException.