How can I elegantly serialize a lambda?
For example, the code below throws a NotSerializableException
. How can I fix it without creating a Seriali
The same construction can be used for method references. For example this code:
import java.io.Serializable;
public class Test {
static Object bar(String s) {
return "make serializable";
}
void m () {
SAM s1 = (SAM & Serializable) Test::bar;
SAM s2 = (SAM & Serializable) t -> "make serializable";
}
interface SAM {
Object action(String s);
}
}
defines a lambda expression and a method reference with a serializable target type.