Why should all Comparators be Serializable?

John Zukowski

If you pass a Comparator that doesn't implement Serializable into a collection like a TreeMap, that collection will automatically be non-serializable. It is best to flag all Comparator implementations with the Serializable marker interface. These Comparator implementations are typically stateless utility classes, not maintaining any additional information, though it wouldn't hurt to verify that no extra information is maintained between accesses.