I know that we can use the function as Serializablewhere we need it.
However, I would like to move this casting to a generic method to make the usage code less cluttered. I am unable to create such a method.
My particular problem is that the map below is not Serializable:
final Map<MyObject, String> map =
new TreeMap<>(Comparator.comparing(MyObject::getCode));
I can fix this using:
final Map<MyObject, String> map =
new TreeMap<>(Comparator.comparing((Function<MyObject, String> & Serializable) MyObject::getCode));
But I would like to be able to do something like:
final Map<MyObject, String> map =
new TreeMap<>(Comparator.comparing(makeSerializable(MyObject::getCode)));
public static <T, U> Function<T, U> makeSerializable(Function<T, U> function) {
return (Function<T, U> & Serializable) function;
}
This is fine for the compiler, but at runtime I get ClassCastException:
java.lang.ClassCastException: SerializableTest$$Lambda$1/801197928 cannot be cast to java.io.Serializable
I also tried the following options, without success:
public static <T extends Serializable, U extends Serializable> Function<T, U> makeSerializable(Function<T, U> function) {
return (Function<T, U> & Serializable) function;
}
public static <T, U> Function<T, U> makeSerializable2(Function<T, U> function) {
return (Function<T, U> & Serializable) t -> function.apply(t);
}
Is it possible to create such a method?
Implementation MyObject:
static class MyObject implements Serializable {
private final String code;
MyObject(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}