Why should a Comparator implement Serializable?
Solution 1
This is not just an Android thing, the Java SDK has the same recommendation:
Note: It is generally a good idea for comparators to also implement java.io.Serializable, as they may be used as ordering methods in serializable data structures (like TreeSet, TreeMap). In order for the data structure to serialize successfully, the comparator (if provided) must implement Serializable.
So the idea is that because a TreeMap is serializable, and the TreeMap can contain a Comparator, it would be good if the Comparator is also serializable. The same applies for all elements in the collection.
You can safely ignore this unless you use serialization in that way.
Solution 2
Serializeable is a blank interface. It does not contain any methods. So, to implement it, all you need to say is implements Serializable
in a class. It's not a huge burden on you. If you extend Comparator
, you don't even need to implement Serializable
because the super class does that for you, and then you don't need to do anything at all to implement Serializable
.
When something implements Serializable
, that means the object can be turned into a byte array at will. This is used for transmission over the Internet, storage in a file, etc. Speaking very roughly, the way serialization works for an object, by default, is to take every object referenced by the object you're trying to serialize, turn each such object into a byte array (i.e. invoke serialization on it recursively), and concatenate the byte arrays to produce a byte array that represents the overall object.
Now, why should a Comparator
implement Serializable
? Let's say you wish to serialize a TreeMap
or some other ordered Collection
. The goal of serialization is to provide a complete representation of an object. Collections like TreeMap
have a Comparator
object in them, so to be able to produce a byte array that captures every aspect of such collections, you need to be able to save the Comparator
as a byte array too. Hence, Comparator
needs to be Serializable
so that other things can be properly serialized.
Solution 3
This should help you out : http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
Note: It is generally a good idea for comparators to implement java.io.Serializable, as they may be used as ordering methods in serializable data structures (like TreeSet, TreeMap). In order for the data structure to serialize successfully, the comparator (if provided) must implement Serializable.
Solution 4
To serialize an object in Java, both these conditions should be satisfied:
- The class to which the instance belongs to must implement java.io.Serializable.
- The members of the class should be serializable. If one or more of the members are not to be serialized, they should be marked as transient.
When any data structure uses a Comparator and you want that data structure to be serializable, point 2 (mentioned above) compels the comparator to implement serializable.
Related videos on Youtube
Comments
-
Bjorn almost 2 years
New to Java. Learning it while working on an Android app. I am implementing a Comparator to sort a list of files and the android docs say that a Comparator should implement Serializable:
It is recommended that a Comparator implements Serializable.
This is the Serializable interface here.
I just want to sort a list of files. Why should I implement this or what even is the reason why it should be for any Comparator?
-
irreputable over 12 yearswhy would Android care about serialization at all?
-
hansvb over 12 years@irreputable: Why wouldn't you use serialization on Android?
-
-
hansvb over 12 years"If you extend Comparator": Comparator is an interface (and does not extend Serializable). "to implement it, all you need to say is implements Serializable in a class" you also need to think about if that is the right thing to do. for example all instance fields also need to be serializable (or you need to take some measures).