intermediateFunctional Programming

What are UnaryOperator and BinaryOperator, and how do they differ from Function and BiFunction?

UnaryOperator<T> is a specialization of Function<T, T> where the input and output types are the same — used for operations like String::toUpperCase that transform a value into the same type. BinaryOperator<T> is a specialization of BiFunction<T, T, T> for combining two values of the same type into one, which is exactly the shape Stream.reduce() expects (e.g. Integer::sum).

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to Why can a poor hashCode() implementation destroy HashMap performance?← Back to all Core Java questions