Skip to content
Tutorials Center
My learning

Topics

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.

Java
UnaryOperator<String> trim = String::strip;
BinaryOperator<Integer> max = Math::max;

Try it

Java UnaryOperator in practice

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.

Java BinaryOperator, minBy and maxBy

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

Java
@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 UnaryOperator is a Function, so andThen and compose are inherited, but they return a plain Function, not a UnaryOperator.
  • reduce() needs associativity. Integer::sum, Math::max and 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 throws IllegalStateException.
  • Use primitive operators such as IntBinaryOperator with IntStream.reduce() to avoid boxing.
  • Type inference usually allows (a, b) -> a + b for BinaryOperator<Integer>, but not mixed types: that needs a BiFunction.
  • replaceAll applies the operator to every element and replaces it; on an immutable list it throws UnsupportedOperationException.