Well, the map
method doesn't expect a StringChanger
implementation. It expects a Function
implementation.
What you can do is create an implementation of your StringChanger
interface, and pass a method reference of your implementation to map
:
StringChanger sc = new StringChanger() {
@Override
public String change(String o) {
return o.trim();
}
};
Stream.of("hello", "world")
.map(sc::change)
.forEach(System.out::println);
EDIT:
In order to assign an implementation of one functional interface to a different functional interface reference, you can assign a method reference of the source functional interface's method :
MyConsumer i3 = i::accept;
IntConsumer i4 = i2::doSomething;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…