In open/src/java.base/share/classes/java/lang/String.java, the UNICODE_CASEFOLD_ORDER comparator is exposed as a public static field, but its implementing class (FoldCaseComparator) does not implement Serializable. This prevents collections or objects using the comparator from being serialized, leading to NotSerializableException at runtime. For example the following code will throw it:
TreeSet<String> set = new TreeSet<>(UNICODE_CASEFOLD_ORDER);
set.add("Foo");
set.add("bar");
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test.ser"))) {
out.writeObject(set);
}
The comparator may implement Serializable to align with CaseInsensitiveComparator.
TreeSet<String> set = new TreeSet<>(UNICODE_CASEFOLD_ORDER);
set.add("Foo");
set.add("bar");
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test.ser"))) {
out.writeObject(set);
}
The comparator may implement Serializable to align with CaseInsensitiveComparator.