Why should a Comparator implement Serializable?

前端 未结 5 485
陌清茗
陌清茗 2021-02-02 06:46

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 Seri

相关标签:
5条回答
  • 2021-02-02 07:20

    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.

    0 讨论(0)
  • 2021-02-02 07:24

    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.

    0 讨论(0)
  • 2021-02-02 07:25

    i have seen in Java 5 API Link to java 5 APT which stated that by implementing Comparator doesn't mean to implement Serializable interface anyways, so one has to explicitly pay attention to get Serializable in some customarily created Comparator class

    0 讨论(0)
  • 2021-02-02 07:32

    To serialize an object in Java, both these conditions should be satisfied:

    1. The class to which the instance belongs to must implement java.io.Serializable.
    2. 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.

    0 讨论(0)
  • 2021-02-02 07:36

    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.

    0 讨论(0)
提交回复
热议问题