List.transform () returns a transformed representation of the original list. From the list of Lists.transform () javadoc :
The returned list always implements Serializable, but serializes only if the fromList and function are serializable.
When serializing a converted view, you actually serialize the source list as well as the function. In your case, this fails because your source list is not serializable (since it contains non-serializable graph elements). But it can also fail, because the function does not implement serializable.
By the way, there is a little trick for creating serializable functions without verbosity. Instead of this:
private static final class MyFunction extends Function<String, String> implements Serializable { private static final MyFunction INSTANCE = new MyFunction(); @Override public String apply(String input) { return "[" + input + "]"; } private Object readResolve() { return INSTANCE; } private static final long serialVersionUID = 1; }
You can use a singleton enumeration template that is much less verbose, and you get serialized for free (since enums are serializable). It also ensures that your function is single:
// enum singleton pattern private enum MyFunction implements Function<String, String> { INSTANCE; @Override public String apply(String input) { return "[" + input + "]"; } }
source share