I want to implicitly cast my own interface implementation to a Java8 function.
My code:
import java.util.stream.Stream;
@FunctionalInterface
interface StringChanger {
String change(String o);
}
public class A {
public static void main(String[] args) {
Stream.of("hello", "world")
.map(new StringChanger() {
@Override
public String change(String o) {
return o.trim();
}
})
.forEach(System.out::println);
}
}
Why does the cast not work?
I'm getting this exception:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method map(Function<? super String,? extends R>) in the type Stream<String> is not applicable for the arguments (Trimmer)
at A.main(A.java:13)
See Question&Answers more detail:os