Why should a Comparator implement Serializable?

前端 未结 5 486
陌清茗
陌清茗 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: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.

提交回复
热议问题