How does apply() method work in Java?
Regarding the UnaryOperator functional interface, I have read the documentation, it says
@FunctionalInterface
public interface UnaryOperator<T> extends Function<T,T>
Represents an operation on a single operand that produces a result of the same type as its operand. This is a specialization of Function for the case where the operand and result are of the same type.
This is a functional interface whose functional method is Function.apply(Object)
.
What is the functional method?
I have the following class which implements UnaryOperator interface.
public class Uppercase implements UnaryOperator<String> {
@Override
public String apply(String s) {
return s.trim().toUpperCase();
}
}
In the method call,
new Stream().map(new Uppercase())
It converts whatever input stream into uppercase. My question is, does the apply()
method in Uppercase class get called automatically? ( Is it like the toString()
method is called automatically by the println()
method?)
I couldn't find any example without calling apply()
method explicitly. So please help me understand what happened here.